Code Monkey home page Code Monkey logo

vue-test-utils-mocha-webpack-example's Introduction

THIS REPOSITORY IS DEPRECATED

Note: This example is outdated. It's now recommended to scaffold your project with Vue CLI 3 which provides out-of-the-box configurations for unit testing.

vue-test-utils-mocha-example

Example project using mocha-webpack and vue-test-utils

This is based on the vue-cli webpack-simple template. Test-specific changes include:

Additional Dependencies

  • vue-test-utils
  • mocha & mocha-webpack
  • jsdom & jsdom-global (for setting up DOM environment in tests)
  • webpack-node-externals (for excluding NPM deps from test bundle)
  • expect (for assertions)
  • nyc & babel-plugin-istanbul (for coverage)

Additional Configuration

package.json

Added test script and setting for nyc:

{
  // ...
  "scripts": {
    // ...
    "test": "cross-env NODE_ENV=test nyc mocha-webpack --webpack-config webpack.config.js --require test/setup.js test/**/*.spec.js"
  },
  "nyc": {
    "include": [
      "src/**/*.(js|vue)"
    ],
    "instrument": false,
    "sourceMap": false
  }
}

webpack.config.js

Added test-specific configs:

if (process.env.NODE_ENV === 'test') {
  // exclude NPM deps from test bundle
  module.exports.externals = [require('webpack-node-externals')()]
  // use inline source map so that it works with mocha-webpack
  module.exports.devtool = 'inline-cheap-module-source-map'
}

test/setup.js

Global setup for tests. This is run first with mocha-webpack's --require flag.

// setup JSDOM
require('jsdom-global')()

// make expect available globally
global.expect = require('expect')

.babelrc

Added "plugins": ["istanbul"]:

{
  "env": {
    // ...
    "test": {
      "plugins": ["istanbul"]
    }
  }
}

Build Setup

# install dependencies
npm install

# serve with hot reload at localhost:8080
npm run dev

# build for production with minification
npm run build

# run unit tests
npm test

For detailed explanation on how things work, consult the docs for vue-test-utils.

vue-test-utils-mocha-webpack-example's People

Contributors

38elements avatar dbschwartz avatar eddyerburgh avatar haru01 avatar linusborg avatar mr-chick avatar yyx990803 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-test-utils-mocha-webpack-example's Issues

broken link readme.md

https://vue-test-utils.vuejs.org/en/guides/testing-SFCs-with-mocha-webpack.html should be https://vue-test-utils.vuejs.org/guides/#testing-single-file-components-with-mocha-webpack

Missing `List.vue` file in coverage report

Hello there!

I pulled the code, ran npm install and List.vue does not appear in the coverage report files. Is that expected?

 WEBPACK  Compiled successfully in 476ms

 MOCHA  Testing...



  List.vue
    ✓ renders li for each item in props.items

  Message
    ✓ renders props.msg when passed
    ✓ renders default message if not passed a prop

  MessageToggle.vue
    ✓ toggles msg passed to Message when button is clicked


  4 passing (26ms)

 MOCHA  Tests completed successfully

----------------------------|----------|----------|----------|----------|----------------|
File                        |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------------------------|----------|----------|----------|----------|----------------|
All files                   |      100 |      100 |      100 |      100 |                |
 Message.js                 |      100 |      100 |      100 |      100 |                |
 MessageToggle.vue?d4b1857c |      100 |      100 |      100 |      100 |                |
----------------------------|----------|----------|----------|----------|----------------|

please update to recent vue-loader with vueloaderplugin

I have problems generating a correct lcov report using SFC. The source path in lcov.info is wrong.

If I use for webpack devtool: "eval", I get:
SF:C:/src/main/frontend/js/components/VueComponent.vue?vue&type=script&lang=js&

If I use for webpack devtool: "inline-source-map", I get:
SF:C:\src\main\frontend\js\components\js\components\VueComponent.vue

The correct path is actually:
C:\src\main\frontend\js\components\VueComponent.vue

It would be very helpful to have an example using the latest vue-loader.

MessageToggle.vue test failed

Hello, i run the code, but the test can't pass 100%, this is the error log:

MessageToggle.vue
1) toggles msg passed to Message when button is clicked

3 passing (35ms)
1 failing

  1. MessageToggle.vue toggles msg passed to Message when button is clicked:
    Error: [vue-test-utils]: find did not return Component, cannot call hasProp() on empty Wrapper
    at throwError (node_modules_vue-test-utils@1.0.0-beta.11@vue-test-utils\dist\vue-test-utils.js:11:9)
    at ErrorWrapper.hasProp (node_modules_vue-test-utils@1.0.0-beta.11@vue-test-utils\dist\vue-test-utils.js:602:3)
    at Context.it (.tmp\mocha-webpack\1519815705782\webpack:\test\MessageToggle.spec.js:12:1)
    at callFn (node_modules_mocha@3.5.3@mocha\lib\runnable.js:348:21)
    at Test.Runnable.run (node_modules_mocha@3.5.3@mocha\lib\runnable.js:340:7)
    at Runner.runTest (node_modules_mocha@3.5.3@mocha\lib\runner.js:443:10)
    at node_modules_mocha@3.5.3@mocha\lib\runner.js:549:12
    at next (node_modules_mocha@3.5.3@mocha\lib\runner.js:361:14)
    at node_modules_mocha@3.5.3@mocha\lib\runner.js:371:7
    at next (node_modules_mocha@3.5.3@mocha\lib\runner.js:295:14)
    at Immediate. (node_modules_mocha@3.5.3@mocha\lib\runner.js:339:5)

if you delete the package-lock.json and npm install tests do not work

When you clone the project "npm run test" works. However if I delete the package-lock.json and perform an "npm i" to reinstall all the modules(a new lock file gets created) I get the following error:

TypeError: Super expression must either be null or a function

I was trying to use this guide to setup mocha based unit tests but was getting errors because I installed the node modules that were needed to run but my own project was throwing that error. As I troubleshooted I ended up finding out it was the package-lock.json file. I think this is important because users will be installing mocha unit testing dependencies and it will not run on their projects.

Critical vulnerabilities in the setup

Hi there, I wanted to try this example, and run npm install.
It replies:
found 195 vulnerabilities (185 low, 5 moderate, 3 high, 2 critical)
Maybe this could be fixed?
Best
Agata

--watch mode stops reporting all test results on code/test updates

Hi!

I've just cloned repo and ran the tests and all is good when I run npm t. However, I'd like to run using watch mode but when I add the --watch switch to the test command, the first run is good and all 4 tests run but when I update either the code or a spec file, it will only run 1 of the tests from that point forward.

Are others seeing this behavior? Likely an issue in mocha-webpack but I didn't see an open issue in their queue around this. Anyway, I thought I'd report it here first and at least check if others are also seeing.

Thanks!

Code coverage in vue-test-utils-mocha-webpack-example only covers JavaScript not vue template file

Hello,

I was able to run code coverage to html by changing the test command in my scripts to be:

//package.json
{...
scripts: {
....
"test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha-webpack --webpack-config webpack.config.js --require test/setup.js test/**/*.spec.js"
...
}

and in webpack.config.js changing module.exports.devtool to 'eval' from 'inline-cheap-module-source-map'.

However, when I look at the html coverage it looks like it only covers the Javascript part of the .vue file and not the template:

image

I noticed this was the vase in vue-test-utils-karma-example as well. I tried running coverage in vue-test-utils-jest-example but all the coverage results are 0% for some reason. Any help would be greatly appreciated! Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.