1

I went over the documentation of cypress and i did not find any details about how to test if a error was thrown in javascript like:
throw new Error('my error');
Question: Is there a solution to test if a error wast thrown like i showed above, or to test the message?

Asking
  • 3,487
  • 11
  • 51
  • 106
  • Could this answer your question? https://stackoverflow.com/questions/66154889/cypress-how-to-properly-detect-for-js-errors-in-a-page – Skip Mar 24 '22 at 09:57
  • @Skip, no, i want maybe to find a solution to mock the `throw new Error` using Cypress. Could you help? Because when i run tests they failed because of the error. – Asking Mar 24 '22 at 10:13

1 Answers1

3

Such an error is an Uncaught exception from your application

When Cypress detects an uncaught exception in your application, it will fail the currently running test.

You can turn off this behavior globally or conditionally with the uncaught:exception event. Please see the Catalog of Events for examples.

Cypress.on('uncaught:exception', (err, runnable) => {
  expect(err.message).to.eq('my error')
  return false
})