Check if an error has been written to the console

前端 未结 4 1778
自闭症患者
自闭症患者 2021-02-04 02:41

I\'m trying to find a way to check if an error has been written to the console when running a cypress unit test.

I know how to log something to the console



        
4条回答
  •  [愿得一人]
    2021-02-04 03:16

    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);
      });
    });
    

提交回复
热议问题