Imagine the scenario: one of your Cypress tests fails. You are not sure why yet, so to avoid blocking the pipeline, you skip the test using it.skip
. You even put a comment above the test with the GitHub issue link while you investigate.
1 | // SKIP https://github.com/acme/project/issues/1 |
What happens next? The number of skipped tests slowly grows. Unless you periodically come back, modify the spec file to unskip the test and try running it locally or via CI, the skipped tests stay ignored. There is a danger here. The skipped tests might get out of sync with the application and would have to be rewritten again.
🎁 You can find the full source code for this blog post in the repo bahmutov/skip-vs-required-tag.
A better way
Here is my solution to this problem. Use @bahmutov/cy-grep plugin and put those tests under required tag rather than skipping them.
1 | $ npm i -D @bahmutov/cy-grep |
Configure the plugin following its README instructions
1 | const { defineConfig } = require('cypress') |
Add to your support file
1 | import registerCypressGrep from '@bahmutov/cy-grep/src/support' |
Now mark each test that you want to skip by default with the required tag @skip
1 | // SKIP https://github.com/acme/project/issues/1 |
Now let's run all tests skipping the one above.
1 | $ npx cypress run |
The entire spec.cy.js
was skipped because it only had it('deletes an item', { requiredTags: '@skip' }
test. Now let's run just the tests with the tag @skip
1 | $ npx cypress run --env grepTags=@skip |
We verified that the test "deletes an item" still fails. So it should keep its required tag @skip
for now.
CI workflows
Let's add two GitHub Actions workflows to our repo. One runs all tests and another runs only the tests with the tag @skip
1 | name: ci |
1 | # a workflow to run all skipped tests nightly |
When the "skipped" workflow finishes, we simply see that it failed.
Hmm, we need a better workflow summary to see which tests are now passing and which tests are still failing.
GitHub Actions test summary
We can use another plugin I wrote bahmutov/cypress-json-results to output GitHub Actions summary to show the individual test results.
1 | $ npm i -D cypress-json-results |
Include the plugin from the cypress.config.js
file
1 | const { defineConfig } = require('cypress') |
Run the tests on GitHub Actions. The main workflow runs passing tests and reports success.
Meanwhile we have fixed a mistake in one of the "skipped" tests and fixed it (maybe). These tests are still ignored when the main workflow runs.
1 | // SKIP https://github.com/acme/project/issues/1 |
Let's trigger the "skipped" workflow to verify our fix.
We verified that the test "updates an item" from the spec file "cypress/e2e/spec.cy.js" is working and we can remove the requiredTags: '@skip'
from its settings.
Tip: we could probably check if any tests we expect to be failing are passing using my plugin cypress-expect when running the skipped workflow.
Links
- demo source repo bahmutov/skip-vs-required-tag
- @bahmutov/cy-grep plugin
- Cypress reusable GitHub workflows
- bahmutov/cypress-json-results plugin
- 🎓 my Cypress Plugins course