|
| 1 | +# Using with Jest |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +To test SFCs, we need to compile the files before running them in node. |
| 6 | + |
| 7 | +## Setting up Jest |
| 8 | + |
| 9 | +The first thing to do is install Jest: |
| 10 | + |
| 11 | +```bash |
| 12 | +$ npm install --save-dev jest |
| 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 | + "test": "jest" |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +As you probably want to use latest javascript capabilities inside your specs though, it's recommendable to enable `babel` for the project. |
| 27 | + |
| 28 | + |
| 29 | +```bash |
| 30 | +$ npm install --save-dev babel babel-jest babel-preset-env |
| 31 | +``` |
| 32 | + |
| 33 | +*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)* |
| 34 | + |
| 35 | +We need to add a `.babelrc` file, to tell babel to use the env preset: |
| 36 | + |
| 37 | +```json |
| 38 | +{ |
| 39 | + "presets": ["env"] |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +By default, Jest doesn't recognize .vue files. So we need to tell Jest how to handle them. To do that we need to install jest-vue, which preprocesses .vue files: |
| 44 | + |
| 45 | +``` |
| 46 | +npm install --sav-dev jest-vue |
| 47 | +``` |
| 48 | + |
| 49 | +In our `package.json`, we need to add a field to tell Jest how to treat .vue files: |
| 50 | +```json |
| 51 | +// package.json |
| 52 | +{ |
| 53 | + "jest": { |
| 54 | + "moduleFileExtensions": [ |
| 55 | + "js", |
| 56 | + "json", |
| 57 | + "vue" |
| 58 | + ], |
| 59 | + "transform": { |
| 60 | + "^.+\\.js$": "<rootDir>/node_modules/babel-jest", |
| 61 | + ".*\\.(vue)$": "<rootDir>/node_modules/jest-vue" |
| 62 | + }, |
| 63 | + "mapCoverage": true |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +That's pretty much everything necessary to do before writing the first specs. |
| 69 | + |
| 70 | +### Where should my tests live |
| 71 | + |
| 72 | +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. |
| 73 | + |
| 74 | +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. |
| 75 | + |
| 76 | +### Example Spec |
| 77 | + |
| 78 | +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 |
| 79 | +enjoy writing specs. |
| 80 | + |
| 81 | +```js |
| 82 | +import { mount } from 'vue-test-utils' |
| 83 | +import Component from './component' |
| 84 | + |
| 85 | +describe('Component', () => { |
| 86 | + test('is a Vue instance', () => { |
| 87 | + const wrapper = mount(Component) |
| 88 | + expect(wrapper.isVueInstance()).toBeTruthy() |
| 89 | + }) |
| 90 | +}) |
| 91 | +``` |
| 92 | + |
| 93 | +### Resources |
| 94 | + |
| 95 | +- Examples and slides from Vue Conf 2017 - https://github.com/codebryo/vue-testing-with-jest-conf17 |
| 96 | +- Jest - https://facebook.github.io/jest/ |
| 97 | +- Babel preset env - https://github.com/babel/babel-preset-env |
0 commit comments