|
| 1 | +# Testing single file components with mocha |
| 2 | + |
| 3 | +We need to compile single file components (SFCs) to run them in mocha. |
| 4 | + |
| 5 | +We can make use of a package called mocha-webpack, that does exactly that. It compiles source files in webpack before running mocha. |
| 6 | + |
| 7 | +## Setting up mocha-webpack |
| 8 | + |
| 9 | +The first thing to do is install mocha, mocha webpack, webpack and webpack loaders: |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install --save-dev vue-test-utils mocha mocha-webpack webpack webpack-node-externals vue vue-loader css-loader babel-loader |
| 13 | +``` |
| 14 | + |
| 15 | +Next we need to define a unit script in our `package.json`. |
| 16 | + |
| 17 | +```json |
| 18 | +// package.json |
| 19 | +{ |
| 20 | + "scripts": { |
| 21 | + "unit": "mocha-webpack --webpack-config build/webpack.conf.js test --recursive --require test/setup.js" |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +This script tells mocha-webpack to get the webpack config from build/webpack.conf.js, run all test files in the test directory and run test/setup.js before the tests. |
| 27 | + |
| 28 | +Let's create the setup.js script first. |
| 29 | + |
| 30 | +Vue Test Utils requires a browser environment to run. We can set one up using browser-env (a wrapper around JSDOM). |
| 31 | + |
| 32 | +Let's install that: |
| 33 | + |
| 34 | +``` |
| 35 | +npm install --save-dev browser-env |
| 36 | +``` |
| 37 | + |
| 38 | +Create a test/setup.js file and paste the following code in: |
| 39 | + |
| 40 | +``` |
| 41 | +require('browser-env')(); |
| 42 | +``` |
| 43 | + |
| 44 | +This adds a browser environment to node, so that Vue Test Utils can run correctly. |
| 45 | + |
| 46 | +Next, we need to install babel and configure it so we can use ES6 features in our JavaScript: |
| 47 | + |
| 48 | +```bash |
| 49 | +npm install --save-dev babel-core babel-preset-env |
| 50 | +``` |
| 51 | + |
| 52 | +and create a .babelrc file in the root of the project, that tells babel to use the env preset: |
| 53 | + |
| 54 | +```json |
| 55 | +// .babelrc |
| 56 | +{ |
| 57 | + "presets": ["env"] |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +*babel-preset-env allows compiling the JS based on the browsers you plan to support. Get more info here: [babel-preset-env](https://github.com/babel/babel-preset-env)* |
| 62 | + |
| 63 | +Now we need to create a webpack config file. Create a build/webpack.conf.js file, and add the following code: |
| 64 | + |
| 65 | +```js |
| 66 | +const nodeExternals = require('webpack-node-externals') |
| 67 | + |
| 68 | +module.exports = { |
| 69 | + module: { |
| 70 | + rules: [ |
| 71 | + { |
| 72 | + test: /\.vue$/, |
| 73 | + loader: 'vue-loader' |
| 74 | + }, |
| 75 | + { |
| 76 | + test: /\.js$/, |
| 77 | + loader: 'babel-loader', |
| 78 | + exclude: /node_modules/ |
| 79 | + } |
| 80 | + ] |
| 81 | + }, |
| 82 | + externals: [nodeExternals()], |
| 83 | + devtool: '#eval-source-map' |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +That's the setup done. Now to add a test. |
| 88 | + |
| 89 | + |
| 90 | +## Adding a test |
| 91 | + |
| 92 | +Create a file in src named Counter.vue. |
| 93 | + |
| 94 | +Paste the following code in src/Counter.vue: |
| 95 | + |
| 96 | + |
| 97 | +```vue |
| 98 | +<template> |
| 99 | + <div> |
| 100 | + {{ count }} |
| 101 | + <button @click="increment">Increment</button> |
| 102 | + </div> |
| 103 | +</template> |
| 104 | +
|
| 105 | +<script> |
| 106 | +export default { |
| 107 | + data () { |
| 108 | + return { |
| 109 | + count: 0 |
| 110 | + } |
| 111 | + }, |
| 112 | +
|
| 113 | + methods: { |
| 114 | + increment () { |
| 115 | + this.count++ |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +</script> |
| 120 | +``` |
| 121 | + |
| 122 | +And create a test file—`test/Counter.spec.js`. Paste the code below into the file: |
| 123 | + |
| 124 | +```js |
| 125 | +import { shallow } from 'vue-test-utils' |
| 126 | +import { expect } from 'chai' |
| 127 | +import Counter from '../src/Counter.vue' |
| 128 | + |
| 129 | +describe('Counter.vue', () => { |
| 130 | + it('increments count when button is clicked', () => { |
| 131 | + const wrapper = shallow(Counter) |
| 132 | + wrapper.find('button').trigger('click') |
| 133 | + expect(wrapper.find('div').text()).to.contain('1') |
| 134 | + }) |
| 135 | +}) |
| 136 | +``` |
| 137 | + |
| 138 | +Notice we're using expect from chai to make our assertion. We need to install chai before running the tests: |
| 139 | + |
| 140 | +``` |
| 141 | +npm install --save-dev chai |
| 142 | +``` |
| 143 | + |
| 144 | +And now we can run the test: |
| 145 | + |
| 146 | +``` |
| 147 | +npm run unit |
| 148 | +``` |
| 149 | + |
| 150 | +Great, the tests are running! |
| 151 | + |
| 152 | +### Resources |
| 153 | + |
| 154 | +- [Example project using mocha-webpack](https://github.com/eddyerburgh/vue-test-utils-mocha-example) |
0 commit comments