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
+37-4Lines changed: 37 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -124,7 +124,7 @@ Example `.babelrc`:
124
124
}
125
125
```
126
126
127
-
###Snapshot Testing
127
+
## Snapshot Testing
128
128
129
129
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).
130
130
@@ -149,13 +149,46 @@ Then configure it in `package.json`:
149
149
}
150
150
```
151
151
152
-
###Placing Test Files
152
+
## Placing Test Files
153
153
154
154
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.
155
155
156
156
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.
157
157
158
-
### Example Spec
158
+
## Coverage
159
+
160
+
Jest can be used to generate coverage reports in multiple formats. The following is a simple example to get started with:
161
+
162
+
Extend your `jest` config (usually in `package.json` or `jest.config.js`) with the [collectCoverage](https://facebook.github.io/jest/docs/en/configuration.html#collectcoverage-boolean) option, and then add the [collectCoverageFrom](https://facebook.github.io/jest/docs/en/configuration.html#collectcoveragefrom-array) array to define the files for which coverage information should be collected. You'll also want [mapCoverage](https://facebook.github.io/jest/docs/en/configuration.html#mapcoverage-boolean) to be `true`, for coverage data to be accurate.
163
+
164
+
```json
165
+
{
166
+
"jest": {
167
+
// ...
168
+
"collectCoverage": true,
169
+
"collectCoverageFrom": [
170
+
"**/*.{js,vue}",
171
+
"!**/node_modules/**"
172
+
],
173
+
"mapCoverage": true
174
+
}
175
+
}
176
+
```
177
+
178
+
This will enable coverage reports with the [default coverage reporters](https://facebook.github.io/jest/docs/en/configuration.html#coveragereporters-array-string). You can customise these with the `coverageReporters` option:
179
+
180
+
```json
181
+
{
182
+
"jest": {
183
+
// ...
184
+
"coverageReporters": ["html", "text-summary"]
185
+
}
186
+
}
187
+
```
188
+
189
+
Further documentation can be found in the [Jest configuration documentation](https://facebook.github.io/jest/docs/en/configuration.html#collectcoverage-boolean), where you can find options for coverage thresholds, target output directories, etc.
190
+
191
+
## Example Spec
159
192
160
193
If you are familiar with Jasmine, you should feel right at home with Jest's [assertion API](https://facebook.github.io/jest/docs/en/expect.html#content):
161
194
@@ -171,7 +204,7 @@ describe('Component', () => {
171
204
})
172
205
```
173
206
174
-
###Resources
207
+
## Resources
175
208
176
209
-[Example project for this setup](https://github.com/vuejs/vue-test-utils-jest-example)
177
210
-[Examples and slides from Vue Conf 2017](https://github.com/codebryo/vue-testing-with-jest-conf17)
0 commit comments