question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React boundary catches an error but fails the test if test continues

See original GitHub issue

Cypress v4.5.0

Example repo https://github.com/bahmutov/test-custom-error-boundary

npm install
npm run dev

Run the integration spec - one will fail and another is successful

Screen Shot 2020-05-01 at 10 45 49 AM

The interesting part: the custom React boundary catches the error, and if use the app manually after the test is finished, it works just fine without throwing

describe('app', () => {
  // but then fails the test
  it('catches an error', () => {
    cy.visit('http://localhost:3000')
    cy.get('.pokemonName-input').type('unknown')
    cy.contains('Submit').click()
    // currently fails
    cy.contains('Try again').should('be.visible')
  })

  it('catches an error and all is good', () => {
    cy.visit('http://localhost:3000')
    cy.get('.pokemonName-input').type('unknown')
    cy.contains('Submit').click()
    // you can click "Try again manually"
  })
})

The second test is happy and no errors leak

Screen Shot 2020-05-01 at 10 57 02 AM

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
rafahuamancommented, Aug 25, 2020

I found this description of what React does.

https://github.com/facebook/react/issues/12897#issuecomment-410036991

In development environment, React uses a trick: caught exceptions are thrown inside a fake DOM event which makes them reported by window.onerror, but then React actually catches them so that they don’t propagate up (in case there is an error boundary — otherwise they are rethrown).

In production errors caught by error boundaries stay caught.

5reactions
chrisbreidingcommented, May 6, 2020

It looks like React still bubbles up the error to window.onerror, which is where Cypress catches it and why it fails the test. You can even see that it displays Cypress’s error because we return that error from window.onerror.

It happens as a result of submitting the unknown pokemon name, but in the second test, it’s too late to trigger failing the test, since it has already passed. It only fails the first test because there are further commands.

There’s room for debate on this, but I think Cypress is doing the right thing. The error hits window.onerror, so it’s natural for Cypress to think that something went wrong. That’s why Cypress gives you a way to turn off failing on uncaught exceptions:

// in test that exercises error boundary
cy.on('uncaught:exception', () => false)

Another option is defining your own window.onerror handler in your app code. Then it overrides Cypress’s handler and, so, won’t fail the test.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Boundaries - React
Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI...
Read more >
Catching Errors in React with Error Boundaries
Error boundaries is a great way to make your application fail gracefully, and even contain errors to parts of your application while the...
Read more >
How to test React ErrorBoundary - Stack Overflow
React is tested enough and that ensures that whenever a error is thrown it will be caught. Therefore there are only test two...
Read more >
React Error Boundaries: Complete Guide - Meticulous
While catching errors before they hit production is ideal, some of them, such as network errors, might slip through testing and impact your ......
Read more >
React Error Handling And Reporting With Error Boundary And ...
In this article, we'll explore the concept of error boundaries in a React application. We'll work through an example app to see how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found