Skip to content

Commit 7a573cf

Browse files
Next.js: Fix react-dom/test-utils aliasing
1 parent b00a8ce commit 7a573cf

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

code/frameworks/nextjs/src/config/webpack.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const configureConfig = async ({
3333
);
3434
}
3535
if (tryResolve('next/dist/compiled/react-dom')) {
36-
addScopedAlias(baseConfig, 'react-dom$', 'next/dist/compiled/react-dom');
36+
addScopedAlias(baseConfig, 'react-dom', 'next/dist/compiled/react-dom');
3737
}
3838

3939
setupRuntimeConfig(baseConfig, nextConfig);

code/renderers/react/src/act-compat.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22
// https://github.com/testing-library/react-testing-library/blob/3dcd8a9649e25054c0e650d95fca2317b7008576/src/act-compat.js
33
import * as React from 'react';
44

5-
import * as DeprecatedReactTestUtils from 'react-dom/test-utils';
6-
75
declare const globalThis: {
86
IS_REACT_ACT_ENVIRONMENT: boolean;
97
};
108

9+
let reactDomTestUtils;
10+
11+
try {
12+
reactDomTestUtils = require('react-dom/test-utils');
13+
} catch (e) {
14+
reactDomTestUtils = {
15+
act: async (cb: (() => void) | (() => Promise<void>)) => {
16+
await cb();
17+
},
18+
};
19+
}
20+
1121
// @ts-expect-error act might not be available in some versions of React
12-
const reactAct = typeof React.act === 'function' ? React.act : DeprecatedReactTestUtils.act;
22+
const reactAct = typeof React.act === 'function' ? React.act : reactDomTestUtils.act;
1323

1424
export function setReactActEnvironment(isReactActEnvironment: boolean) {
1525
globalThis.IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment;

0 commit comments

Comments
 (0)