Skip a test with a conditional in Cypress?

Answered
looper003 asked this question 2 months ago
looper003 on Mar 15

In my Cypress tests, I want to add an if () conditionally where a test will be ran or skipped based on a true or false value.

For instance, if I have this test:

it('should focus on the todo input field', function () {
  cy.focused().should('have.class', 'new-todo')
})

How could I skip it? Is there a cy.skip() or this.skip() method in Cypress?

2 suggested answers
nick 16 days ago

You can simply wrap your test in an if statement like you would in any JavaScript code:

if (condition) {
  it('should focus on the todo input field', function () {
    cy.focused().should('have.class', 'new-todo')
  })
}
0 replies
yaboy01 16 days ago

Cypress has @cypress/skip-test NPM package you could use for this.

It provides skipOn() and onlyOn() methods you could use.

For example, you could skip a test if Cypress is using the Firefox browser:

it('skips on Firefox', () => {
  skipOn('firefox')
})
0 replies
Answered