Janning Vygen
Janning Vygen

Reputation: 9192

Cypress - let test fail if console.error occurs

We would like to let any test in Cypress fail if console.error is called. Sometimes we have errors in the console log which does not let the test fail. We always have to look into the console window to check any error messages

Upvotes: 0

Views: 1670

Answers (1)

Janning Vygen
Janning Vygen

Reputation: 9192

Just add this to your support/index.js

let consoleSpy;
Cypress.on('window:before:load', (win) => {
    consoleSpy = cy.spy(win.console, "error")
})
afterEach(() => {
    // consoleSpy can be null if test failed already in beforeEach 
    if (consoleSpy) {
      expect(consoleSpy).not.to.be.called
    }
})

It will run on each Test and will check if console.error was called and will then let the test fail.

Upvotes: 4

Related Questions