Skip to content

Commit 633153e

Browse files
committed
docs: add using with jest guide
1 parent 9ef35c8 commit 633153e

File tree

2 files changed

+22
-60
lines changed

2 files changed

+22
-60
lines changed

docs/en/SUMMARY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@
3737
* [setProps](api/wrapper-array/setProps.md)
3838
* [trigger](api/wrapper-array/trigger.md)
3939
* [selectors](api/selectors.md)
40+
* guides
41+
* [using with Jest](guides/using-with-jest.md)
42+
* [testing compiled components with Jest](guides/using-compiled-components-with-jest.md)
4043
* [common gotchas](common-gotchas.md)

docs/en/guides/using-with-jest.md

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,40 @@
1-
# Using with Jest
1+
Jest and Vue Test Utils work great together.
22

3-
## Setup Jest
3+
To get started, you need to install Jest and Vue Test Utils:
44

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
95
```
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
207
```
218

22-
or
9+
Inside package.json, create an npm script that runs Jest:
2310

24-
```bash
25-
$ yarn jest
2611
```
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"
4814
}
4915
```
5016

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:
6318

6419
```js
6520
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+
}
6726

6827
describe('Component', () => {
69-
test('is a Vue instance', () => {
28+
test('renders "Hello, World!"', () => {
7029
const wrapper = mount(Component)
71-
expect(wrapper.isVueInstance()).toBeTruthy()
30+
expect(wrapper.text()).toBe('Hello, World!')
7231
})
7332
})
7433
```
7534

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+
```
8139

40+
If you want to test .vue files, please see the testing SFCs with Jest guide.

0 commit comments

Comments
 (0)