Skip to content

Commit 78672c1

Browse files
robcresswelleddyerburgh
authored andcommitted
docs: add code coverage docs for Jest (#295)
Fixes #165
1 parent 2819e51 commit 78672c1

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

docs/en/guides/testing-SFCs-with-jest.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Example `.babelrc`:
124124
}
125125
```
126126

127-
### Snapshot Testing
127+
## Snapshot Testing
128128

129129
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).
130130

@@ -149,13 +149,46 @@ Then configure it in `package.json`:
149149
}
150150
```
151151

152-
### Placing Test Files
152+
## Placing Test Files
153153

154154
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.
155155

156156
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.
157157

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
159192

160193
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):
161194

@@ -171,7 +204,7 @@ describe('Component', () => {
171204
})
172205
```
173206

174-
### Resources
207+
## Resources
175208

176209
- [Example project for this setup](https://github.com/vuejs/vue-test-utils-jest-example)
177210
- [Examples and slides from Vue Conf 2017](https://github.com/codebryo/vue-testing-with-jest-conf17)

0 commit comments

Comments
 (0)