React boundary catches an error but fails the test if test continues
See original GitHub issueCypress 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
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
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found this description of what React does.
https://github.com/facebook/react/issues/12897#issuecomment-410036991
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 fromwindow.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: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.