Skip to content

feat: set flag from plugins task that they were registered #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,35 @@ workflows:
node ../../scripts/only-covered main.js
working_directory: examples/support-files

- cypress/run:
attach-workspace: true
name: example-use-plugins-and-support
requires:
- cypress/install
# there are no jobs to follow this one
# so no need to save the workspace files (saves time)
no-workspace: true
command: npx cypress run --project examples/use-plugins-and-support
# store screenshots and videos
store_artifacts: true
post-steps:
- run: cat examples/use-plugins-and-support/.nyc_output/out.json
# store the created coverage report folder
# you can click on it in the CircleCI UI
# to see live static HTML site
- store_artifacts:
path: examples/use-plugins-and-support/coverage
# make sure the examples captures 100% of code
- run:
command: npx nyc report --check-coverage true --lines 100
working_directory: examples/use-plugins-and-support
- run:
name: Check code coverage 📈
command: |
node ../../scripts/check-coverage main.js
node ../../scripts/only-covered main.js
working_directory: examples/use-plugins-and-support

- publish:
filters:
branches:
Expand All @@ -266,3 +295,4 @@ workflows:
- example-ts-example
- example-same-folder
- example-support-files
- example-use-plugins-and-support
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ Register tasks in your `cypress/plugins/index.js` file

```js
module.exports = (on, config) => {
on('task', require('@cypress/code-coverage/task'))
require('@cypress/code-coverage/task')(on, config)
// IMPORTANT to return the config object
// with the any changed environment variables
return config
}
```

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

```js
module.exports = (on, config) => {
on('task', require('@cypress/code-coverage/task'))
require('@cypress/code-coverage/task')(on, config)
on('file:preprocessor', require('@cypress/code-coverage/use-babelrc'))
return config
}
```

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

```js
module.exports = (on, config) => {
on('task', require('@cypress/code-coverage/task'))
require('@cypress/code-coverage/task')(on, config)
on(
'file:preprocessor',
require('@cypress/code-coverage/use-browserify-istanbul')
)
return config
}
```

Expand Down Expand Up @@ -349,6 +354,37 @@ npm run dev:no:coverage
- [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
- [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.

## Migrations

### v2 to v3

Change the plugins file `cypress/plugins/index.js`

```js
// BEFORE
module.exports = (on, config) => {
on('task', require('@cypress/code-coverage/task'))
}
// AFTER
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
}
```

**Tip:** we include [plugins.js](plugins.js) file you can point at from your code in simple cases. From your `cypress.json` file:

```json
{
"pluginsFile": "node_modules/@cypress/code-coverage/plugins",
"supportFile": "node_modules/@cypress/code-coverage/support"
}
```

See [examples/use-plugins-and-support](examples/use-plugins-and-support)

## Debugging

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:
Expand Down
5 changes: 4 additions & 1 deletion cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = (on, config) => {
on('task', require('../../task'))
require('../../task')(on, config)

// also use .babelrc file when bundling spec files
// to get the code coverage from unit tests
Expand All @@ -9,4 +9,7 @@ module.exports = (on, config) => {
// or use browserify and just push babel-plugin-istanbul
// directory to the list of babelify plugins
// on('file:preprocessor', require('../../use-browserify-istanbul'))

// IMPORTANT to return the config object with changed environment variable
return config
}
5 changes: 4 additions & 1 deletion examples/before-all-visit/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module.exports = (on, config) => {
on('task', require('../../../../task'))
require('../../../../task')(on, config)
// IMPORTANT to return the config object
// with the any changed environment variables
return config
}
5 changes: 4 additions & 1 deletion examples/before-each-visit/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module.exports = (on, config) => {
on('task', require('../../../../task'))
require('../../../../task')(on, config)
// IMPORTANT to return the config object
// with the any changed environment variables
return config
}
3 changes: 2 additions & 1 deletion examples/same-folder/plugins.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = (on, config) => {
on('task', require('../../task'))
require('../../task')(on, config)
on('file:preprocessor', require('../../use-babelrc'))
return config
}
3 changes: 2 additions & 1 deletion examples/support-files/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = (on, config) => {
on('task', require('../../../../task'))
require('../../../../task')(on, config)
on('file:preprocessor', require('../../../../use-babelrc'))
return config
}
3 changes: 2 additions & 1 deletion examples/ts-example/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = (on, config) => {
on('task', require('../../../../task'))
require('../../../../task')(on, config)
return config
}
5 changes: 5 additions & 0 deletions examples/use-plugins-and-support/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# example: use-plugins-and-support

Using included plugins and support files

See [cypress.json](cypress.json) file
5 changes: 5 additions & 0 deletions examples/use-plugins-and-support/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"pluginsFile": "../../plugins",
"supportFile": "../../support",
"fixturesFolder": false
}
18 changes: 18 additions & 0 deletions examples/use-plugins-and-support/cypress/integration/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference types="cypress" />
describe('coverage information', () => {
beforeEach(() => {
cy.visit('index.html')
})

it('calls add', () => {
cy.window()
.invoke('add', 2, 3)
.should('equal', 5)
})

it('calls sub', () => {
cy.window()
.invoke('sub', 2, 3)
.should('equal', -1)
})
})
4 changes: 4 additions & 0 deletions examples/use-plugins-and-support/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<body>
Page body
<script src="main-instrumented.js"></script>
</body>
146 changes: 146 additions & 0 deletions examples/use-plugins-and-support/main-instrumented.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/use-plugins-and-support/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
window.add = (a, b) => a + b

window.sub = (a, b) => a - b
4 changes: 4 additions & 0 deletions examples/use-plugins-and-support/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions examples/use-plugins-and-support/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "use-plugins-and-support",
"description": "Using included plugins and support files",
"devDependencies": {},
"scripts": {
"cy:open": "../../node_modules/.bin/cypress open"
}
}
13 changes: 13 additions & 0 deletions plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// common Cypress plugin file you can point at to have the
// code coverage tasks registered correctly. From your "cypress.json" file
// {
// "pluginsFile": "@cypress/code-coverage/plugins",
// "supportFile": "@cypress/code-coverage/support"
// }
//
module.exports = (on, config) => {
require('./task')(on, config)
// IMPORTANT to return the config object
// with the any changed environment variables
return config
}
Loading