Check if an error has been written to the console

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

    Edit: the following does not directly log to terminal when in headless mode, but it nonetheless fails the test on AUT's console.error and displays the error message indirectly, even in the headless terminal, which may be what you want.

    I'm not sure exactly what you mean, but let's go through all the places where an output can be logged in cypress, and how to handle several cases.

    First, an overview:

    1. To log into the command log, you use:

      // from inside your test
      cy.log('foo');
      
    2. To log into devTools console:

      // from inside your test
      console.log('bar');
      
    3. To log into terminal, you need to log from within the Cypress' node process:

      // from within e.g. your plugin/index.js file
      console.log('baz');
      

    How to log AUT's errors to Terminal, Command Log, and fail the test

    (note, AUT here stands for Application under test, meaning your application).

    I'm also using ansicolor package to make the error red-colored in the terminal, which is optional.

    // plugins/index.js
    const ansi = require(`ansicolor`);
    module.exports = ( on ) => {
        on(`task`, {
            error ( message ) {
                // write the error in red color
                console.error( ansi.red(message) );
                // play `beep` sound for extra purchase
                process.stdout.write(`\u0007`);
                return null;
            }
        });
    };
    

    Note: using internal cy.now() command to work around Cypress' tendency to throw Cypress detected that you returned a promise when it (IMO) shouldn't.

    (adapted from https://github.com/cypress-io/cypress/issues/300#issuecomment-438176246)

    // support/index.js or your test file
    Cypress.on(`window:before:load`, win => {
    
        cy.stub( win.console, `error`, msg => {
            // log to Terminal
            cy.now(`task`, `error`, msg );
            // log to Command Log & fail the test
            throw new Error( msg );
        });
    });
    
    0 讨论(0)
  • 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);
      });
    });
    
    0 讨论(0)
  • 2021-02-04 03:18

    Currently there is no straightforward way to do what you are asking but there have been some good discussions on how best to get this information. I copied one solution here but if you follow the github link you can see other solutions proposed.

    This snippet was taken from the github issue found here: https://github.com/cypress-io/cypress/issues/300

    Just FYI the one easy solution is just to spy on console functions. cy.window().then((win) => { cy.spy(win.console, "log") })

    That will print a command log every time that function is called, and you could also then assert what has been logged.

    Another option depending on why you want to assert that something went wrong is to print the error out under the tests in headless mode. The VP of engineering created an NPM package that does this for you.

    Cypress-failed-log

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题