From c79128fa0f3b45880e340e18cb72c59ce6d24306 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 10 Jul 2019 17:11:23 -0400 Subject: [PATCH] chore: explain how to exclude files from coverage report (#35) --- README.md | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 27ad2c9e..d440560f 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,63 @@ if (global.__coverage__) { 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 +## Exclude code + +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: + +### Exclude "else" branch + +When running code only during Cypress tests, the "else" branch will never be hit. Thus we should exclude it from the branch coverage computation: + +```js +// expose "store" reference during tests +/* istanbul ignore else */ +if (window.Cypress) { + window.store = store +} +``` + +### Exclude next logical statement + +Often needed to skip a statement + +```js +/* istanbul ignore next */ +if (global.__coverage__) { + require('@cypress/code-coverage/middleware/express')(app) +} +``` + +Or a particular `switch` case + +```js +switch (foo) { + case 1: /* some code */; break; + /* istanbul ignore next */ + case 2: // really difficult to hit from tests + someCode(); +} +``` + +### Exclude files and folders + +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. + +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` + +```json +{ + "nyc": { + "include": [ + "app/**/*.js" + ], + "exclude": [ + "app/util.js" + ] + } +} +``` + ## Links - Read the [Cypress code coverage guide](http://on.cypress.io/code-coverage) diff --git a/package.json b/package.json index 69ef54f0..1825c5e9 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "cy:open": "cypress open", "dev": "start-test 1234 cy:open", "semantic-release": "semantic-release", - "test:ci": "start-test 1234" + "test:ci": "start-test 1234", + "report:coverage": "nyc report --reporter=html" }, "peerDependencies": { "cypress": "*",