From 9a64089ada4eb09b07327483b442b10c1429b81b Mon Sep 17 00:00:00 2001 From: eps1lon Date: Thu, 29 Aug 2024 13:50:21 +0200 Subject: [PATCH] Add test illustrating how much better testing errors in React 19 is --- package.json | 6 +++--- src/__tests__/error-handlers.js | 24 ++++++++++++++++++++++++ tests/toWarnDev.js | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 8bfbeecc..623b236d 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "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" }, @@ -66,8 +66,8 @@ "@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": { diff --git a/src/__tests__/error-handlers.js b/src/__tests__/error-handlers.js index b663c626..60db1410 100644 --- a/src/__tests__/error-handlers.js +++ b/src/__tests__/error-handlers.js @@ -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 '../' @@ -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() + }).toThrow('Boom!') + } else { + expect(() => { + expect(() => { + render() + }).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!') diff --git a/tests/toWarnDev.js b/tests/toWarnDev.js index 2aae39f0..3005125e 100644 --- a/tests/toWarnDev.js +++ b/tests/toWarnDev.js @@ -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,