You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/guides/testing-SFCs-with-jest.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Testing single file components with Jest
1
+
# Testing Single File Components with Jest
2
2
3
3
> An example project for this setup is available on [GitHub](https://github.com/vuejs/vue-test-utils-jest-example).
4
4
@@ -54,7 +54,7 @@ Next, create a `jest` block in `package.json`:
54
54
}
55
55
```
56
56
57
-
> **Note:**`jest-vue` currently does not support all the features of `vue-loader`, for example custom block support and style loading. To use them, read the guide on [testing SFCs with Mocha + webpack](./testing-SFCs-with-mocha-webpack.md).
57
+
> **Note:**`jest-vue` currently does not support all the features of `vue-loader`, for example custom block support and style loading. In addition, some webpack-specific features such as code-splitting are not supported either. To use them, read the guide on [testing SFCs with Mocha + webpack](./testing-SFCs-with-mocha-webpack.md).
# Testing single file components with Mocha + webpack
1
+
# Testing Single File Components with Mocha + webpack
2
2
3
-
We need to compile single file components (SFCs) to run them in Mocha.
3
+
Another strategy for testing SFCs is compiling all our tests via webpack and then run it in a test runner. The advantage of this approach is that it gives us full support for all webpack and `vue-loader` features, so we don't have to make compromises in our source code.
4
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.
5
+
You can technically use any test runner you like and manually wire things together, but we've found [`mocha-webpack`](https://github.com/zinserjan/mocha-webpack) to provide a very streamlined experience for this particular task.
6
6
7
-
## Setting up mocha-webpack
7
+
## Setting Up `mocha-webpack`
8
8
9
-
The first thing to do is install Mocha, mocha webpack, webpack and webpack loaders:
9
+
We will assume you are starting with a setup that already has webpack, vue-loader and Babel properly configured - e.g. the `webpack-simple` template scaffolded by `vue-cli`.
10
+
11
+
The first thing to do is installing test dependencies:
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.
28
+
A few things to note here:
27
29
28
-
### Setting Up Browser Environment
30
+
- The `--webpack-config` flag specifies the webpack config file to use for the tests. In most cases, this would be identical to the config you use for the actual project with one small tweak. We will talk about this later.
31
+
32
+
- The `--require` flag ensures the file `test/setup.js` is run before any tests, in which we can setup the global environment for our tests to be run in.
33
+
34
+
- The final argument is a glob for the test files to be included in the test bundle.
35
+
36
+
### Extra webpack Configuration
37
+
38
+
#### Externalizing NPM Dependencies
39
+
40
+
In our tests we will likely import a number of NPM dependencies - some of these modules may be written without browser usage in mind and simply cannot be bundled properly by webpack. Another consideration is externalizing dependencies greatly improves test boot up speed. We can externalize all NPM dependencies with `webpack-node-externals`:
Source maps need to be inlined to be picked up by `mocha-webpack`. The recommended config is:
55
+
56
+
```js
57
+
module.exports= {
58
+
// ...
59
+
devtool:"inline-cheap-module-source-map"
60
+
}
61
+
```
29
62
30
-
Let's create the setup.js script first.
63
+
If debugging via IDE, it's also recommended to add the following:
31
64
32
-
`vue-test-utils` requires a browser environment to run. We can set one up using `jsdom-global`, which setups a JSDOM instance and attaches necessary globals to the Node process.
65
+
```js
66
+
module.exports= {
67
+
// ...
68
+
output: {
69
+
// ...
70
+
// use absolute paths in sourcemaps (important for debugging via IDE)
`vue-test-utils` requires a browser environment to run. We can simulate it in Node.js using `jsdom-global`:
35
80
36
81
```bash
37
82
npm install --save-dev jsdom jsdom-global
38
83
```
39
84
40
-
Create a`test/setup.js` file and paste the following code in:
85
+
Then in`test/setup.js`:
41
86
42
87
```js
43
88
require('jsdom-global')()
44
89
```
45
90
46
91
This adds a browser environment to node, so that `vue-test-utils` can run correctly.
47
92
48
-
### Configuring webpack
93
+
### Picking an Assertion Library
49
94
50
-
Now we need to create a webpack config file. In most cases your test config should use the same `module` rules with your projects existing webpack config. We recommend extracting the common config options into a base file and extend it separately for build and testing.
95
+
[Chai](http://chaijs.com/) is a popular assertion library that is commonly used alongside Mocha. You may also want to check out [Sinon](http://sinonjs.org/) for creating spies and stubs.
51
96
52
-
One specific tweak needed for the test config is that we should externalize Node dependencies with `webpack-node-externals`. This significantly speeds up the bundling process.
97
+
Alternatively you can use `expect` which is now part of Jest, and exposes [the exact same API](http://facebook.github.io/jest/docs/en/expect.html#content) in Jest docs.
53
98
54
-
For our example project, the config looks like this:
99
+
We will be using `expect` here and make it globally available so that we don't have to import it in every test:
Notice that we are using `babel-loader` to handle JavaScript. You should already have Babel configured if you are using it in your app, e.g. via a `.babelrc` file. Here `babel-loader` will automatically use the same config file.
115
+
Notice that we are using `babel-loader` to handle JavaScript. You should already have Babel configured if you are using it in your app via a `.babelrc` file. Here `babel-loader` will automatically use the same config file.
81
116
82
117
One thing to note is that if you are using Node 6+, which already supports the majority of ES2015 features, you can configure a separate Babel [env option](https://babeljs.io/docs/usage/babelrc/#env-option) that only transpiles features that are not already supported in the Node version you are using (e.g. `stage-2` or flow syntax support, etc.)
83
118
@@ -114,24 +149,17 @@ And create a test file named `test/Counter.spec.js` with the following code:
114
149
115
150
```js
116
151
import { shallow } from'vue-test-utils'
117
-
import { expect } from'chai'
118
152
importCounterfrom'../src/Counter.vue'
119
153
120
154
describe('Counter.vue', () => {
121
155
it('increments count when button is clicked', () => {
0 commit comments