Skip to content

Commit

Permalink
Add test illustrating how much better testing errors in React 19 is
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Aug 29, 2024
1 parent 668f4d0 commit 9a64089
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@
"kcd-scripts": "^13.0.0",
"npm-run-all": "^4.1.5",
"react": "^18.3.1",
"react-dom": "^18.3.0",
"react-dom": "^18.3.1",
"rimraf": "^3.0.2",
"typescript": "^4.1.2"
},
"peerDependencies": {
"@testing-library/dom": "^10.0.0",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"peerDependenciesMeta": {
"@types/react": {
Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/error-handlers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable jest/no-if */
/* eslint-disable jest/no-conditional-in-test */
/* eslint-disable jest/no-conditional-expect */
import * as React from 'react'
import {render, renderHook} from '../'
Expand All @@ -6,6 +8,28 @@ const isReact19 = React.version.startsWith('19.')

const testGateReact19 = isReact19 ? test : test.skip

test('render errors', () => {
function Thrower() {
throw new Error('Boom!')
}

if (isReact19) {
expect(() => {
render(<Thrower />)
}).toThrow('Boom!')
} else {
expect(() => {
expect(() => {
render(<Thrower />)
}).toThrow('Boom!')
}).toErrorDev([
'Error: Uncaught [Error: Boom!]',
// React retries on error
'Error: Uncaught [Error: Boom!]',
])
}
})

test('onUncaughtError is not supported in render', () => {
function Thrower() {
throw new Error('Boom!')
Expand Down
2 changes: 1 addition & 1 deletion tests/toWarnDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const createMatcherFor = (consoleMethod, matcherName) =>
// doesn't match the number of arguments.
// We'll fail the test if it happens.
let argIndex = 0
format.replace(/%s/g, () => argIndex++)
String(format).replace(/%s/g, () => argIndex++)
if (argIndex !== args.length) {
lastWarningWithMismatchingFormat = {
format,
Expand Down

0 comments on commit 9a64089

Please sign in to comment.