Check if an error has been written to the console

This does exactly what I needed of catching any error in the console and do an assertion of the logs count. Just add the following in cypress/support/index.js

Cypress.on('window:before:load', (win) => {
  cy.spy(win.console, 'error');
  cy.spy(win.console, 'warn');
});

afterEach(() => {
  cy.window().then((win) => {
    expect(win.console.error).to.have.callCount(0);
    expect(win.console.warn).to.have.callCount(0);
  });
});

Leave a Comment