Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Watch console for errors

I have an app using canvas elements, which are difficult to test with javascript but it does throw messages to the console.

How can I watch for errors written to the console?

I tried monkey patching the window console, but it’s not working.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const messages = []

window.console.error = (msg) => {
  messages.push(msg)
})

// actions causing error

expect(messages.length).to.eq(0)

>Solution :

You can watch console messages with Cypress cy.spy(), ref docs

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

// actions causing error

cy.then(() => {  
  expect(spy).not.to.be.called
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading