title: Cypress - how to properly detect for JS errors in a page
body: <p>I am in the process of writing a spec file with N it(s), where each it will visit a specific page of the application and will return the number of errors/warning in the applications.</p>
  <p>I've posted also here: <a href="https://github.com/cypress-io/cypress/issues/4808" rel="nofollow noreferrer">https://github.com/cypress-io/cypress/issues/4808</a></p>
  <p>The solution offered does not seem to work:</p>
  <pre><code>it('should spy window.error', () =&gt; {
    cy.visit('https://www.spiegel.de/', {
      onBeforeLoad(win) {
        cy.spy(win.console, 'error').as('spyWinConsoleError');
        cy.spy(win.console, 'warn').as('spyWinConsoleWarn');
      },
    })
    console.error('questo e errore')
    cy.get('@spyWinConsoleError').should('be.calledOnce');
  });
  </code></pre>
  <p>Any idea?</p>