Check if an error has been written to the console

前端 未结 4 1786
自闭症患者
自闭症患者 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:20

    There have been some updates since the previous answers.

    Because the window is re-created with each cy.visit, Cypress recommends stubbing as a part of the cy.visit command.

    cy.visit('/', {
      onBeforeLoad(win) {
        cy.stub(win.console, 'log').as('consoleLog')
        cy.stub(win.console, 'error').as('consoleError')
      }
    })
    
    //...
    cy.get('@consoleLog').should('be.calledWith', 'Hello World!')
    cy.get('@consoleError').should('be.calledOnce')
    

    For more details see the official FAQ for stubbing out the console: https://docs.cypress.io/faq/questions/using-cypress-faq.html#How-do-I-spy-on-console-log

    And the recipe repository: https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/stubbing-spying__console

提交回复
热议问题