Skip to content

Commit ad11a24

Browse files
eddyerburghkuitos
authored andcommitted
docs: add TypeScript guide (vuejs#876)
1 parent fb2d27a commit ad11a24

6 files changed

+308
-97
lines changed

Diff for: docs/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Vue Test Utils is the official unit testing utility library for Vue.js.
1111
* [Testing SFCs with Mocha + webpack](guides/testing-single-file-components-with-mocha-webpack.md)
1212
* [Testing SFCs with Karma](guides/testing-single-file-components-with-karma.md)
1313
* [Testing Asynchronous Behavior](guides/testing-async-components.md)
14+
* [Using with TypeScript](guides/using-with-typescript.md)
1415
* [Using with Vue Router](guides/using-with-vue-router.md)
1516
* [Using with Vuex](guides/using-with-vuex.md)
1617
* [API](api/)

Diff for: docs/guides/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
!!!include(docs/guides/testing-single-file-components-with-mocha-webpack.md)!!!
99
!!!include(docs/guides/testing-single-file-components-with-karma.md)!!!
1010
!!!include(docs/guides/testing-async-components.md)!!!
11+
!!!include(docs/guides/using-with-typescript.md)!!!
1112
!!!include(docs/guides/using-with-vue-router.md)!!!
1213
!!!include(docs/guides/using-with-vuex.md)!!!

Diff for: docs/guides/testing-single-file-components-with-jest.md

+33-26
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Next, create a `jest` block in `package.json`:
4747
],
4848
"transform": {
4949
// process `*.vue` files with `vue-jest`
50-
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
50+
".*\\.(vue)$": "vue-jest"
5151
}
5252
}
5353
}
@@ -123,31 +123,6 @@ Example `.babelrc`:
123123
}
124124
```
125125

126-
### Snapshot Testing
127-
128-
You can use [`vue-server-renderer`](https://github.com/vuejs/vue/tree/dev/packages/vue-server-renderer) to render a component into a string so that it can be saved as a snapshot for [Jest snapshot testing](https://facebook.github.io/jest/docs/en/snapshot-testing.html).
129-
130-
The render result of `vue-server-renderer` includes a few SSR-specific attributes, and it ignores whitespaces, making it harder to scan a diff. We can improve the saved snapshot with a custom serializer:
131-
132-
``` bash
133-
npm install --save-dev jest-serializer-vue
134-
```
135-
136-
Then configure it in `package.json`:
137-
138-
``` json
139-
{
140-
// ...
141-
"jest": {
142-
// ...
143-
// serializer for snapshots
144-
"snapshotSerializers": [
145-
"<rootDir>/node_modules/jest-serializer-vue"
146-
]
147-
}
148-
}
149-
```
150-
151126
### Placing Test Files
152127

153128
By default, Jest will recursively pick up all files that have a `.spec.js` or `.test.js` extension in the entire project. If this does not fit your needs, it's possible [to change the `testRegex`](https://facebook.github.io/jest/docs/en/configuration.html#testregex-string) in the config section in the `package.json` file.
@@ -202,6 +177,38 @@ describe('Component', () => {
202177
})
203178
```
204179

180+
### Snapshot Testing
181+
182+
When you mount a component with Vue Test Utils, you get access to the root HTML element. This can be saved as a snapshot for [Jest snapshot testing](https://facebook.github.io/jest/docs/en/snapshot-testing.html):
183+
184+
```js
185+
test('renders correctly', () => {
186+
const wrapper = mount(Component)
187+
expect(wrapper.element).toMatchSnapshot()
188+
})
189+
```
190+
191+
We can improve the saved snapshot with a custom serializer:
192+
193+
``` bash
194+
npm install --save-dev jest-serializer-vue
195+
```
196+
197+
Then configure it in `package.json`:
198+
199+
``` json
200+
{
201+
// ...
202+
"jest": {
203+
// ...
204+
// serializer for snapshots
205+
"snapshotSerializers": [
206+
"jest-serializer-vue"
207+
]
208+
}
209+
}
210+
```
211+
205212
### Resources
206213

207214
- [Example project for this setup](https://github.com/vuejs/vue-test-utils-jest-example)

Diff for: docs/guides/using-with-typescript.md

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
## Using with TypeScript
2+
3+
> An example project for this setup is available on [GitHub](https://github.com/vuejs/vue-test-utils-typescript-example).
4+
5+
TypeScript is a popular superset of JavaScript that adds types and classes on top of regular JS. Vue Test Utils includes types in the distributed package, so it works well with TypeScript.
6+
7+
In this guide, we'll walk through how to setup a testing setup for a TypeScript project using Jest and Vue Test Utils from a basic Vue CLI TypeScript setup.
8+
9+
#### Adding TypeScript
10+
11+
First you need to create a project. If you don't have Vue CLI installed, install it globally:
12+
13+
```shell
14+
$ npm install -g @vue/cli-service-global
15+
```
16+
17+
And create a project by running:
18+
19+
```shell
20+
$ vue create hello-world
21+
```
22+
23+
In the CLI prompt, choose to manually select features, select TypeScript, and press enter. This will create a project with TypeScript already configured.
24+
25+
::: tip NOTE
26+
If you want a more detailed guide on setting up Vue with TypeScript, checkout the [TypeScript Vue starter guide](https://github.com/Microsoft/TypeScript-Vue-Starter).
27+
:::
28+
29+
The next step is to add Jest to the project.
30+
31+
#### Setting up Jest
32+
33+
Jest is a test runner developed by Facebook, aiming to deliver a battery-included unit testing solution. You can learn more about Jest on its [official documentation](https://facebook.github.io/jest/).
34+
35+
Install Jest and Vue Test Utils:
36+
37+
```bash
38+
$ npm install --save-dev jest @vue/test-utils
39+
```
40+
41+
Next define a `test:unit` script in `package.json`.
42+
43+
```json
44+
// package.json
45+
{
46+
// ..
47+
"scripts": {
48+
// ..
49+
"test:unit": "jest"
50+
}
51+
// ..
52+
}
53+
```
54+
55+
### Processing SFCs in Jest
56+
57+
To teach Jest how to process `*.vue` files, we need to install and configure the `vue-jest` preprocessor:
58+
59+
``` bash
60+
npm install --save-dev vue-jest
61+
```
62+
63+
Next, create a `jest` block in `package.json`:
64+
65+
``` json
66+
{
67+
// ...
68+
"jest": {
69+
"moduleFileExtensions": [
70+
"js",
71+
"ts",
72+
"json",
73+
// tell Jest to handle `*.vue` files
74+
"vue"
75+
],
76+
"transform": {
77+
// process `*.vue` files with `vue-jest`
78+
".*\\.(vue)$": "vue-jest",
79+
},
80+
"testURL": "http://localhost/"
81+
}
82+
}
83+
```
84+
85+
### Configuring TypeScript for Jest
86+
87+
In order to use TypeScript files in tests, we need to set up Jest to compile TypeScript. For that we need to install `ts-jest`:
88+
89+
``` bash
90+
$ npm install --save-dev ts-jest
91+
```
92+
93+
Next, we need to tell Jest to process JavaScript test files with `ts-jest` by adding an entry under `jest.transform` in `package.json`:
94+
95+
``` json
96+
{
97+
// ...
98+
"jest": {
99+
// ...
100+
"transform": {
101+
// ...
102+
// process `*.ts` files with `ts-jest`
103+
"^.+\\.tsx?$": "ts-jest"
104+
},
105+
// ...
106+
}
107+
}
108+
```
109+
110+
### Placing Test Files
111+
112+
By default, Jest will recursively pick up all files that have a `.spec.js` or `.test.js` extension in the entire project.
113+
114+
To run test files with a `.ts` extension, we need to change the `testRegex` in the config section in the `package.json` file.
115+
116+
Add the following to the `jest` field in `package.json`:
117+
118+
``` json
119+
{
120+
// ...
121+
"jest": {
122+
// ...
123+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$"
124+
}
125+
}
126+
```
127+
128+
Jest recommends creating a `__tests__` directory right next to the code being tested, but feel free to structure your tests as you see fit. Just beware that Jest would create a `__snapshots__` directory next to test files that performs snapshot testing.
129+
130+
### Writing a unit test
131+
132+
Now we've got the project set up, it's time to write a unit test.
133+
134+
Create a `src/components/__tests__/HelloWorld.spec.ts` file, and add the following code:
135+
136+
```js
137+
// src/components/__tests__/HelloWorld.spec.ts
138+
import { shallowMount } from '@vue/test-utils'
139+
import HelloWorld from '../HelloWorld.vue'
140+
141+
describe('HelloWorld.vue', () => {
142+
test('renders props.msg when passed', () => {
143+
const msg = 'new message'
144+
const wrapper = shallowMount(HelloWorld, {
145+
propsData: { msg }
146+
})
147+
expect(wrapper.text()).toMatch(msg)
148+
})
149+
})
150+
```
151+
152+
That's all we need to do to get TypeScript and Vue Test Utils working together!
153+
154+
### Resources
155+
156+
- [Example project for this setup](https://github.com/vuejs/vue-test-utils-typescript-example)
157+
- [Jest](https://facebook.github.io/jest/)

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"vue-router": "^3.0.1",
7070
"vue-server-renderer": "2.5.16",
7171
"vue-template-compiler": "2.5.16",
72-
"vuepress": "^0.10.0",
72+
"vuepress": "^0.13.0",
7373
"vuepress-theme-vue": "^1.0.3",
7474
"vuetify": "^0.16.9",
7575
"vuex": "^3.0.1",

0 commit comments

Comments
 (0)