Skip to content

Commit 288b61d

Browse files
authored
feat: set flag from plugins task that they were registered (#180)
* feat: set flag from plugins task that they were registered BREAKING CHANGE: the task sets an environment variable and needs to return its config. The new registration thus looks like this ```js // your plugins file module.exports = (on, config) => { require('cypress/code-coverage/task')(on, config) // IMPORTANT to return the config object // with the any changed environment variables return config } ``` Support code can check variable `Cypress.env('codeCoverageTasksRegistered')` before calling `cy.task` * update readme * update examples * add small plugins file * update readme * add example using plugins and support * add new example to CI * no need to start server in the example
1 parent ce34d61 commit 288b61d

File tree

19 files changed

+341
-17
lines changed

19 files changed

+341
-17
lines changed

.circleci/config.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,35 @@ workflows:
251251
node ../../scripts/only-covered main.js
252252
working_directory: examples/support-files
253253

254+
- cypress/run:
255+
attach-workspace: true
256+
name: example-use-plugins-and-support
257+
requires:
258+
- cypress/install
259+
# there are no jobs to follow this one
260+
# so no need to save the workspace files (saves time)
261+
no-workspace: true
262+
command: npx cypress run --project examples/use-plugins-and-support
263+
# store screenshots and videos
264+
store_artifacts: true
265+
post-steps:
266+
- run: cat examples/use-plugins-and-support/.nyc_output/out.json
267+
# store the created coverage report folder
268+
# you can click on it in the CircleCI UI
269+
# to see live static HTML site
270+
- store_artifacts:
271+
path: examples/use-plugins-and-support/coverage
272+
# make sure the examples captures 100% of code
273+
- run:
274+
command: npx nyc report --check-coverage true --lines 100
275+
working_directory: examples/use-plugins-and-support
276+
- run:
277+
name: Check code coverage 📈
278+
command: |
279+
node ../../scripts/check-coverage main.js
280+
node ../../scripts/only-covered main.js
281+
working_directory: examples/use-plugins-and-support
282+
254283
- publish:
255284
filters:
256285
branches:
@@ -266,3 +295,4 @@ workflows:
266295
- example-ts-example
267296
- example-same-folder
268297
- example-support-files
298+
- example-use-plugins-and-support

README.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ Register tasks in your `cypress/plugins/index.js` file
2323

2424
```js
2525
module.exports = (on, config) => {
26-
on('task', require('@cypress/code-coverage/task'))
26+
require('@cypress/code-coverage/task')(on, config)
27+
// IMPORTANT to return the config object
28+
// with the any changed environment variables
29+
return config
2730
}
2831
```
2932

@@ -109,8 +112,9 @@ Put the following in `cypress/plugins/index.js` file to use `.babelrc` file
109112

110113
```js
111114
module.exports = (on, config) => {
112-
on('task', require('@cypress/code-coverage/task'))
115+
require('@cypress/code-coverage/task')(on, config)
113116
on('file:preprocessor', require('@cypress/code-coverage/use-babelrc'))
117+
return config
114118
}
115119
```
116120

@@ -122,11 +126,12 @@ If you cannot use `.babelrc` for some reason (maybe it is used by other tools?),
122126

123127
```js
124128
module.exports = (on, config) => {
125-
on('task', require('@cypress/code-coverage/task'))
129+
require('@cypress/code-coverage/task')(on, config)
126130
on(
127131
'file:preprocessor',
128132
require('@cypress/code-coverage/use-browserify-istanbul')
129133
)
134+
return config
130135
}
131136
```
132137

@@ -349,6 +354,37 @@ npm run dev:no:coverage
349354
- [bahmutov/code-coverage-subfolder-example](https://github.com/bahmutov/code-coverage-subfolder-example) shows how to instrument `app` folder using `nyc instrument` as a separate step before running E2E tests
350355
- [bahmutov/docker-with-cypress-included-code-coverage-example](https://github.com/bahmutov/docker-with-cypress-included-code-coverage-example) runs tests inside pre-installed Cypress using [cypress/included:x.y.z](https://github.com/cypress-io/cypress-docker-images/tree/master/included) Docker image and reports code coverage.
351356

357+
## Migrations
358+
359+
### v2 to v3
360+
361+
Change the plugins file `cypress/plugins/index.js`
362+
363+
```js
364+
// BEFORE
365+
module.exports = (on, config) => {
366+
on('task', require('@cypress/code-coverage/task'))
367+
}
368+
// AFTER
369+
module.exports = (on, config) => {
370+
require('@cypress/code-coverage/task')(on, config)
371+
// IMPORTANT to return the config object
372+
// with the any changed environment variables
373+
return config
374+
}
375+
```
376+
377+
**Tip:** we include [plugins.js](plugins.js) file you can point at from your code in simple cases. From your `cypress.json` file:
378+
379+
```json
380+
{
381+
"pluginsFile": "node_modules/@cypress/code-coverage/plugins",
382+
"supportFile": "node_modules/@cypress/code-coverage/support"
383+
}
384+
```
385+
386+
See [examples/use-plugins-and-support](examples/use-plugins-and-support)
387+
352388
## Debugging
353389

354390
This plugin uses [debug](https://github.com/visionmedia/debug) module to output additional logging messages from its [task.js](task.js) file. This can help with debugging errors while saving code coverage or reporting. In order to see these messages, run Cypress from the terminal with environment variable `DEBUG=code-coverage`. Example using Unix syntax to set the variable:

cypress/plugins/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = (on, config) => {
2-
on('task', require('../../task'))
2+
require('../../task')(on, config)
33

44
// also use .babelrc file when bundling spec files
55
// to get the code coverage from unit tests
@@ -9,4 +9,7 @@ module.exports = (on, config) => {
99
// or use browserify and just push babel-plugin-istanbul
1010
// directory to the list of babelify plugins
1111
// on('file:preprocessor', require('../../use-browserify-istanbul'))
12+
13+
// IMPORTANT to return the config object with changed environment variable
14+
return config
1215
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
module.exports = (on, config) => {
2-
on('task', require('../../../../task'))
2+
require('../../../../task')(on, config)
3+
// IMPORTANT to return the config object
4+
// with the any changed environment variables
5+
return config
36
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
module.exports = (on, config) => {
2-
on('task', require('../../../../task'))
2+
require('../../../../task')(on, config)
3+
// IMPORTANT to return the config object
4+
// with the any changed environment variables
5+
return config
36
}

examples/same-folder/plugins.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = (on, config) => {
2-
on('task', require('../../task'))
2+
require('../../task')(on, config)
33
on('file:preprocessor', require('../../use-babelrc'))
4+
return config
45
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = (on, config) => {
2-
on('task', require('../../../../task'))
2+
require('../../../../task')(on, config)
33
on('file:preprocessor', require('../../../../use-babelrc'))
4+
return config
45
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = (on, config) => {
2-
on('task', require('../../../../task'))
2+
require('../../../../task')(on, config)
3+
return config
34
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# example: use-plugins-and-support
2+
3+
Using included plugins and support files
4+
5+
See [cypress.json](cypress.json) file
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"pluginsFile": "../../plugins",
3+
"supportFile": "../../support",
4+
"fixturesFolder": false
5+
}

0 commit comments

Comments
 (0)