Skip to content

Example testing a React component with custom error boundary

Notifications You must be signed in to change notification settings

bahmutov/test-custom-error-boundary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom error boundary example

Downloaded from codesandbox from tweet

npm i -D cypress cypress-react-unit-test

Set cypress/plugins/index.js, cypress/support/index.js and cypress.json files following cypress-react-unit-test README.

Tests in src/PokemonInfo.spec.js and in src/App.spec.js

All good

Mew App test

Trying to fetch unknown pokemon leads to a broken test

Uncaught error

Run

After installing dependencies, to run integration tests yarn run dev. To run component tests just need yarn cypress open.

Observations

$ npm ls webpack
[email protected] /Users/gleb/git/test-custom-error-boundary
└─┬ [email protected]
  └── [email protected]
  • I think because of transient dependencies, the user could get an error when doing react-scripts start, which looks scary and requires .env file with SKIP_PREFLIGHT_CHECK=true. #157
  • index.js included ReactDOM.render(<App />, document.getElementById('root')) which just generated a cryptic error. Had to refactor the app to get around (or comment it out) #158
  • if the user forgets to include cypress-react-unit-test/support from the support file, the error is cryptic #159
  • custom error boundary works, seems to be in Cypress core

Custom error boundary

BUT

We can click the "try again" button ourselves and it triggers the stub, but if we try to wait or do any command from the test, it fails the test

const pokemonName = 'unknown'
const onReset = cy.stub().as('reset')
const ProtectedPokemon = () => (
  <div className="pokemon-info">
    <AppErrorBoundary onReset={onReset} keys={[pokemonName]}>
      <PokemonInfo pokemonName={pokemonName} />
    </AppErrorBoundary>
  </div>
)
mount(<ProtectedPokemon />)
// it catches the error
cy.wait(3000)

Try to wait and the test fails

^^ Note: this is a bug, and it happens even during e2e test in cypress/integration/spec.js. Opened #7196

E2E fails too

About

Example testing a React component with custom error boundary

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published