DEV Community

Discussion on: Failing a Test on console.error in Cypress

Collapse
 
karfau profile image
Christian Bewernitz

I just wanted to mention that you can also very easily enable it for a single test by using the onBeforeLoad option on cy.visit.

In my case I had to rethrow the error with a setTimeout to not interrupt rendering that was currently going on.

  function failOnConsoleError(win: Cypress.AUTWindow) {
    win.console.error = (...args) => {
      // since this is happening in the scope of the application
      // rethrowing directly would cause more error messages
      // we just delay it a bit, cypress will still log it as an uncaught error
      // and causes the test to fail
      setTimeout(() => {
        throw new Error(args.join(', '))
      }, 0);
    }
  }
Enter fullscreen mode Exit fullscreen mode