Skip to content

Commit c79128f

Browse files
authored
chore: explain how to exclude files from coverage report (#35)
1 parent d2be01a commit c79128f

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,63 @@ if (global.__coverage__) {
121121

122122
That should be enough - the code coverage from the server will be requested at the end of the test run and merged with the client-side code coverage, producing a combined report
123123

124+
## Exclude code
125+
126+
You can exclude parts of the code or entire files from the code coverage report. See [Istanbul guide](https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md). Common cases:
127+
128+
### Exclude "else" branch
129+
130+
When running code only during Cypress tests, the "else" branch will never be hit. Thus we should exclude it from the branch coverage computation:
131+
132+
```js
133+
// expose "store" reference during tests
134+
/* istanbul ignore else */
135+
if (window.Cypress) {
136+
window.store = store
137+
}
138+
```
139+
140+
### Exclude next logical statement
141+
142+
Often needed to skip a statement
143+
144+
```js
145+
/* istanbul ignore next */
146+
if (global.__coverage__) {
147+
require('@cypress/code-coverage/middleware/express')(app)
148+
}
149+
```
150+
151+
Or a particular `switch` case
152+
153+
```js
154+
switch (foo) {
155+
case 1: /* some code */; break;
156+
/* istanbul ignore next */
157+
case 2: // really difficult to hit from tests
158+
someCode();
159+
}
160+
```
161+
162+
### Exclude files and folders
163+
164+
See [`nyc` configuration](https://github.com/istanbuljs/nyc#common-configuration-options) and [ include and exclude options](https://github.com/istanbuljs/nyc#using-include-and-exclude-arrays). You can include and exclude files using `minimatch` patterns in `.nycrc` file or using "nyc" object inside your `package.json` file.
165+
166+
For example, if you want to only include files in the `app` folder, but exclude `app/util.js` file, you can set in your `package.json`
167+
168+
```json
169+
{
170+
"nyc": {
171+
"include": [
172+
"app/**/*.js"
173+
],
174+
"exclude": [
175+
"app/util.js"
176+
]
177+
}
178+
}
179+
```
180+
124181
## Links
125182

126183
- Read the [Cypress code coverage guide](http://on.cypress.io/code-coverage)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"cy:open": "cypress open",
1010
"dev": "start-test 1234 cy:open",
1111
"semantic-release": "semantic-release",
12-
"test:ci": "start-test 1234"
12+
"test:ci": "start-test 1234",
13+
"report:coverage": "nyc report --reporter=html"
1314
},
1415
"peerDependencies": {
1516
"cypress": "*",

0 commit comments

Comments
 (0)