Skip to content

Commit a7b83ae

Browse files
authored
feat: get code coverage from component tests (#71)
1 parent a957846 commit a7b83ae

File tree

11 files changed

+3430
-34
lines changed

11 files changed

+3430
-34
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ node_modules/
33
npm-debug.log
44
cypress/screenshots
55
dist
6+
.nyc_output
7+
coverage

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,67 @@ See [cypress.json](cypress.json) in this project.
3737

3838
- need to load images differently to transform relative paths
3939

40+
## Code coverage
41+
42+
### Instrument your code
43+
44+
See [rollup.config.js](rollup.config.js) how you can instrument source files. In short:
45+
46+
```js
47+
// npm i -D rollup-plugin-istanbul
48+
import istanbul from 'rollup-plugin-istanbul'
49+
plugins: [
50+
istanbul({
51+
include: ['cypress/components/**'],
52+
exclude: ['**/*spec.js'],
53+
}),
54+
]
55+
```
56+
57+
In Cypress iframe you should see the code coverage object under `window.__coverage__`.
58+
59+
![Window coverage object](images/window-coverage.png)
60+
61+
### Coverage report
62+
63+
To merge coverage and generate reports we need to use [@cypress/code-coverage](https://github.com/cypress-io/code-coverage) plugin.
64+
65+
```shell
66+
npm i -D @cypress/code-coverage
67+
```
68+
69+
Add it to your [cypress/support/index.js](cypress/support/index.js) file
70+
71+
```js
72+
import '@cypress/code-coverage/support'
73+
```
74+
75+
Add the plugin to your [cypress/plugins/index.js](cypress/plugins/index.js) file
76+
77+
```js
78+
module.exports = (on, config) => {
79+
on('file:preprocessor', require('@bahmutov/cy-rollup'))
80+
require('@cypress/code-coverage/task')(on, config)
81+
// IMPORTANT to return the config object
82+
// with the any changed environment variables
83+
return config
84+
}
85+
```
86+
87+
After the tests finish, you should see messages in the Command Log
88+
89+
![Coverage messages](images/coverage-messages.png)
90+
91+
And find generated reports in `coverage` folder. For example, to open the HTML report
92+
93+
```shell
94+
open coverage/lcov-report/index.html
95+
```
96+
97+
![Coverage report](images/coverage-report.png)
98+
99+
**Warning:** I am not sure the coverage numbers are making 100% sense for Svelte files.
100+
40101
## Svelte v3
41102

42103
This component adaptor is meant for [Svelte v3](https://svelte.dev/blog/svelte-3-rethinking-reactivity). If you need Svelte v2 support, check out branch [svelte-v2](https://github.com/bahmutov/cypress-svelte-unit-test/tree/svelte-v2)

cypress.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
"testFiles": "**/*spec.js",
66
"experimentalComponentTesting": true,
77
"componentFolder": "cypress/components",
8-
"supportFile": false,
98
"nodeVersion": "system"
109
}

cypress/plugins/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
module.exports = (on) => {
1+
module.exports = (on, config) => {
22
// https://github.com/bahmutov/cy-rollup
33
on('file:preprocessor', require('@bahmutov/cy-rollup'))
4+
5+
// https://github.com/cypress-io/code-coverage
6+
require('@cypress/code-coverage/task')(on, config)
7+
// IMPORTANT to return the config object
8+
// with the any changed environment variables
9+
return config
410
}

cypress/support/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@cypress/code-coverage/support'

images/coverage-messages.png

391 KB
Loading

images/coverage-report.png

114 KB
Loading

images/window-coverage.png

257 KB
Loading

0 commit comments

Comments
 (0)