
Skip Dependent Cypress Tests On Failure
Sometimes you want your tests to be dependent on each other. For example, instead of a single long E2E test, I have created small focused "step" tests. Each test starts where the previous test left off; the test isolation flag is set to false.
1 |
import { LoginInfo } from '.' |
🎓 This example comes from one of the lessons in my Testing The Swag Store online course.
If the tests pass, all is good. The tests are quick.
But what happens when we fail to log in? For example, if we accidentally quote the username:
1 |
it('logs in', () => { |
The first test fails, and each test after that also fails - it takes a while to fail, since each command retries.
If a test fails, we want to skip all tests after it. I wrote a plugin cypress-skip-this-test to do precisely that. Install that plugin as a dev dependency and call the exported function before each test in the suite of dependent tests.
1 |
import { skipIfPreviousTestsFailed } from 'cypress-skip-this-test' |
The function skipIfPreviousTestsFailed
checks the parent suite of the current test. If a previous test fails, then the current test is skipped. It is a standard Mocha syntax:
1 |
const getMochaContext = () => cy.state('runnable').ctx |
Simple and effective. You can find even simpler example in the bahmutov/cypress-skip-example repo.