Skip to content

Commit 63ceda3

Browse files
committed
Add test illustrating how much better testing errors in React 19 is
1 parent fd256ec commit 63ceda3

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"kcd-scripts": "^13.0.0",
5959
"npm-run-all": "^4.1.5",
6060
"react": "^18.3.1",
61-
"react-dom": "^18.3.0",
61+
"react-dom": "^18.3.1",
6262
"rimraf": "^3.0.2",
6363
"typescript": "^4.1.2"
6464
},

src/__tests__/error-handlers.js

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable jest/no-if */
2+
/* eslint-disable jest/no-conditional-in-test */
13
/* eslint-disable jest/no-conditional-expect */
24
import * as React from 'react'
35
import {render, renderHook} from '../'
@@ -6,6 +8,28 @@ const isReact19 = React.version.startsWith('19.')
68

79
const testGateReact19 = isReact19 ? test : test.skip
810

11+
test('render errors', () => {
12+
function Thrower() {
13+
throw new Error('Boom!')
14+
}
15+
16+
if (isReact19) {
17+
expect(() => {
18+
render(<Thrower />)
19+
}).toThrow('Boom!')
20+
} else {
21+
expect(() => {
22+
expect(() => {
23+
render(<Thrower />)
24+
}).toThrow('Boom!')
25+
}).toErrorDev([
26+
'Error: Uncaught [Error: Boom!]',
27+
// React retries on error
28+
'Error: Uncaught [Error: Boom!]',
29+
])
30+
}
31+
})
32+
933
test('onUncaughtError is not supported in render', () => {
1034
function Thrower() {
1135
throw new Error('Boom!')

tests/toWarnDev.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const createMatcherFor = (consoleMethod, matcherName) =>
115115
// doesn't match the number of arguments.
116116
// We'll fail the test if it happens.
117117
let argIndex = 0
118-
format.replace(/%s/g, () => argIndex++)
118+
String(format).replace(/%s/g, () => argIndex++)
119119
if (argIndex !== args.length) {
120120
lastWarningWithMismatchingFormat = {
121121
format,

0 commit comments

Comments
 (0)