testing-library / react-testing-library

🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
https://testing-library.com/react
MIT License
19.04k stars 1.11k forks source link

feat: Add support for React error handlers #1354

Open eps1lon opened 2 months ago

eps1lon commented 2 months ago

What:

Alternate to https://github.com/testing-library/react-testing-library/pull/1333

Adds support for onRecoverableError and onCaughtError (React 19 only)

onUncaughtError is not supported yet because there's no use. The render would throw anyway. We're likely going to change that though in React.

Why:

This allows test setups that fail on console errors to silence specific, expected errors in their tests. Especially onCaughtError will simplify testing of error boundaries since it will disable React's default console.error + window.onError dispatch (see docs I'll add soon)

How:

Forward mentioned options to React's createRoot call.

Checklist:

codesandbox-ci[bot] commented 2 months ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 9a64089ada4eb09b07327483b442b10c1429b81b:

Sandbox Source
react-testing-library-examples Configuration