diff --git a/packages/react-dom/src/__tests__/ReactServerRendering-test.js b/packages/react-dom/src/__tests__/ReactServerRendering-test.js
index c071d3ff47b5..4a78a17117af 100644
--- a/packages/react-dom/src/__tests__/ReactServerRendering-test.js
+++ b/packages/react-dom/src/__tests__/ReactServerRendering-test.js
@@ -147,7 +147,7 @@ describe('ReactDOMServer', () => {
it('should throw with silly args', () => {
expect(
ReactDOMServer.renderToString.bind(ReactDOMServer, {x: 123}),
- ).toThrowError(
+ ).toThrow(
'Objects are not valid as a React child (found: object with keys {x})',
);
});
@@ -155,7 +155,7 @@ describe('ReactDOMServer', () => {
it('should throw prop mapping error for an with invalid props', () => {
expect(() => {
ReactDOMServer.renderToString();
- }).toThrowError(
+ }).toThrow(
'The `style` prop expects a mapping from style properties to values, not ' +
"a string. For example, style={{marginRight: spacing + 'em'}} when using JSX.",
);
@@ -290,7 +290,7 @@ describe('ReactDOMServer', () => {
it('should throw with silly args', () => {
expect(
ReactDOMServer.renderToStaticMarkup.bind(ReactDOMServer, {x: 123}),
- ).toThrowError(
+ ).toThrow(
'Objects are not valid as a React child (found: object with keys {x})',
);
});
@@ -715,7 +715,7 @@ describe('ReactDOMServer', () => {
,
);
- }).toThrowError(/Cannot assign to read only property.*/);
+ }).toThrow(/Cannot assign to read only property.*/);
} else {
expect(
ReactDOMServer.renderToStaticMarkup(
@@ -966,7 +966,7 @@ describe('ReactDOMServer', () => {
expect(() => {
ReactDOMServer.renderToString();
- }).toThrow("Cannot read property 'world' of undefined");
+ }).toThrow("Cannot read properties of undefined (reading 'world')");
});
it('should warn when class contextType is undefined', () => {
@@ -981,7 +981,7 @@ describe('ReactDOMServer', () => {
expect(() => {
ReactDOMServer.renderToString();
- }).toThrow("Cannot read property 'world' of undefined");
+ }).toThrow("Cannot read properties of undefined (reading 'world')");
assertConsoleErrorDev([
'Foo defines an invalid contextType. ' +
'contextType should point to the Context object returned by React.createContext(). ' +
@@ -1007,7 +1007,7 @@ describe('ReactDOMServer', () => {
expect(() => {
ReactDOMServer.renderToString();
- }).toThrow("Cannot read property 'hello' of undefined");
+ }).toThrow("Cannot read properties of undefined (reading 'hello')");
assertConsoleErrorDev([
'Foo defines an invalid contextType. ' +
'contextType should point to the Context object returned by React.createContext(). ' +
@@ -1026,7 +1026,7 @@ describe('ReactDOMServer', () => {
expect(() => {
ReactDOMServer.renderToString();
- }).toThrow("Cannot read property 'world' of undefined");
+ }).toThrow("Cannot read properties of undefined (reading 'world')");
assertConsoleErrorDev([
'Foo defines an invalid contextType. ' +
'contextType should point to the Context object returned by React.createContext(). ' +
diff --git a/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js b/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js
index f48a78de4afb..425b3aade7e1 100644
--- a/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js
+++ b/packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js
@@ -234,7 +234,7 @@ describe('ReactHooksWithNoopRenderer', () => {
it('throws when called outside the render phase', async () => {
expect(() => useState(0)).toThrow(
- "Cannot read property 'useState' of null",
+ "Cannot read properties of null (reading 'useState')",
);
assertConsoleErrorDev([
'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' +
diff --git a/scripts/jest/matchers/toThrow.js b/scripts/jest/matchers/toThrow.js
deleted file mode 100644
index 204a2fc713ca..000000000000
--- a/scripts/jest/matchers/toThrow.js
+++ /dev/null
@@ -1,50 +0,0 @@
-'use strict';
-
-// V8 uses a different message format when reading properties of null or undefined.
-// Older versions use e.g. "Cannot read property 'world' of undefined"
-// Newer versions use e.g. "Cannot read properties of undefined (reading 'world')"
-// This file overrides the built-in toThrow() matches to handle both cases,
-// enabling the React project to support Node 12-16 without forking tests.
-
-const toThrowMatchers = require('expect/build/toThrowMatchers').default;
-const builtInToThrow = toThrowMatchers.toThrow;
-
-// Detect the newer stack format:
-let newErrorFormat = false;
-try {
- null.test();
-} catch (error) {
- if (error.message.includes('Cannot read properties of null')) {
- newErrorFormat = true;
- }
-}
-
-// Detect the message pattern we need to rename:
-const regex = /Cannot read property '([^']+)' of (.+)/;
-
-// Massage strings (written in the older format) to match the newer format
-// if tests are currently running on Node 16+
-function normalizeErrorMessage(message) {
- if (newErrorFormat) {
- const match = message.match(regex);
- if (match) {
- return `Cannot read properties of ${match[2]} (reading '${match[1]}')`;
- }
- }
-
- return message;
-}
-
-function toThrow(value, expectedValue) {
- if (typeof expectedValue === 'string') {
- expectedValue = normalizeErrorMessage(expectedValue);
- } else if (expectedValue instanceof Error) {
- expectedValue.message = normalizeErrorMessage(expectedValue.message);
- }
-
- return builtInToThrow.call(this, value, expectedValue);
-}
-
-module.exports = {
- toThrow,
-};
diff --git a/scripts/jest/setupTests.js b/scripts/jest/setupTests.js
index 28586d94b593..48e1a51f9436 100644
--- a/scripts/jest/setupTests.js
+++ b/scripts/jest/setupTests.js
@@ -47,7 +47,6 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
expect.extend({
...require('./matchers/reactTestMatchers'),
- ...require('./matchers/toThrow'),
});
// We have a Babel transform that inserts guards against infinite loops.
diff --git a/scripts/jest/spec-equivalence-reporter/setupTests.js b/scripts/jest/spec-equivalence-reporter/setupTests.js
index a561fb4b4d1d..e02839788325 100644
--- a/scripts/jest/spec-equivalence-reporter/setupTests.js
+++ b/scripts/jest/spec-equivalence-reporter/setupTests.js
@@ -60,5 +60,4 @@ afterEach(assertConsoleLogsCleared);
expect.extend({
...require('../matchers/reactTestMatchers'),
- ...require('../matchers/toThrow'),
});