|
1 |
| -# Using with Jest |
| 1 | +Jest and Vue Test Utils work great together. |
2 | 2 |
|
3 |
| -## Setup Jest |
| 3 | +To get started, you need to install Jest and Vue Test Utils: |
4 | 4 |
|
5 |
| -Jest is a test runner developed by Facebook, aiming to come with everything included to get started. [Learn more about Jest](https://facebook.github.io/jest/) So let's get started: |
6 |
| - |
7 |
| -```bash |
8 |
| -$ yarn add jest |
9 | 5 | ```
|
10 |
| - |
11 |
| -will install jest and makes it available to be executed through yarn or a package script. |
12 |
| - |
13 |
| -```json |
14 |
| -// package.json |
15 |
| -{ |
16 |
| - "scripts": { |
17 |
| - "test": "jest" |
18 |
| - } |
19 |
| -} |
| 6 | +npm install --save-dev jest vue-test-utils |
20 | 7 | ```
|
21 | 8 |
|
22 |
| -or |
| 9 | +Inside package.json, create an npm script that runs Jest: |
23 | 10 |
|
24 |
| -```bash |
25 |
| -$ yarn jest |
26 | 11 | ```
|
27 |
| - |
28 |
| -As you probably want to use latest javascript capabilities inside your specs though, it's recommendable to enable `babel` for the project. |
29 |
| - |
30 |
| - |
31 |
| -```bash |
32 |
| -$ yarn add babel babel-jest babel-preset-env |
33 |
| -``` |
34 |
| - |
35 |
| -If you’ve not heard of babel-preset-env, it basically 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) |
36 |
| - |
37 |
| -Be sure to update your `.babelrc` file accordingly |
38 |
| - |
39 |
| -```json |
40 |
| -{ |
41 |
| - "presets": [ |
42 |
| - ["env", { |
43 |
| - "targets": { |
44 |
| - "browsers": ["last 2 versions", "safari >= 7"] |
45 |
| - } |
46 |
| - }] |
47 |
| - ] |
| 12 | +"scripts": { |
| 13 | + "unit": "jest" |
48 | 14 | }
|
49 | 15 | ```
|
50 | 16 |
|
51 |
| -That's pretty much everything necessary to do before writing the first specs. |
52 |
| - |
53 |
| -### Where should my tests live |
54 |
| - |
55 |
| -By default, jest will pick up all files that have a `.spec.js` or `.test.js` extension. If this does not fit your needs, it's possible [to chang the testRegex](https://facebook.github.io/jest/docs/en/configuration.html#testregex-string) in the config section in the `package.json` file. |
56 |
| - |
57 |
| -Jest recommends to create a `__spec__` folder, but feel free to do as you like. Just know ahead of time, that when using the [snapshot](https://facebook.github.io/jest/docs/en/snapshot-testing.html#content) feature, snapshots will get stored in a `__snapshot__` folder. |
58 |
| - |
59 |
| -### Example Spec |
60 |
| - |
61 |
| -If you are familiar with Jasmine, or similar test libraries you should feel at home in Jest right away. Many useful assertions are in place, so |
62 |
| -enjoy writing specs. |
| 17 | +Add a `Component.spec.js` file: |
63 | 18 |
|
64 | 19 | ```js
|
65 | 20 | import { mount } from 'vue-test-utils'
|
66 |
| -import Component from './component' |
| 21 | + |
| 22 | +const Component = { |
| 23 | + name: 'component', |
| 24 | + render: (h) => h('div', 'Hello, World!') |
| 25 | +} |
67 | 26 |
|
68 | 27 | describe('Component', () => {
|
69 |
| - test('is a Vue instance', () => { |
| 28 | + test('renders "Hello, World!"', () => { |
70 | 29 | const wrapper = mount(Component)
|
71 |
| - expect(wrapper.isVueInstance()).toBeTruthy() |
| 30 | + expect(wrapper.text()).toBe('Hello, World!') |
72 | 31 | })
|
73 | 32 | })
|
74 | 33 | ```
|
75 | 34 |
|
76 |
| -### Resources |
77 |
| - |
78 |
| -- Examples and slides from Vue Conf 2017 - https://github.com/codebryo/vue-testing-with-jest-conf17 |
79 |
| -- Jest - https://facebook.github.io/jest/ |
80 |
| -- Babel preset env - https://github.com/babel/babel-preset-env |
| 35 | +And run the tests from the command line: |
| 36 | +``` |
| 37 | +npm run unit |
| 38 | +``` |
81 | 39 |
|
| 40 | +If you want to test .vue files, please see the testing SFCs with Jest guide. |
0 commit comments