Skip to content

Commit

Permalink
Next.js: Fix react-dom/test-utils aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Sep 5, 2024
1 parent b00a8ce commit 52c264f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion code/frameworks/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const configureConfig = async ({
);
}
if (tryResolve('next/dist/compiled/react-dom')) {
addScopedAlias(baseConfig, 'react-dom$', 'next/dist/compiled/react-dom');
addScopedAlias(baseConfig, 'react-dom', 'next/dist/compiled/react-dom');
}

setupRuntimeConfig(baseConfig, nextConfig);
Expand Down
16 changes: 13 additions & 3 deletions code/renderers/react/src/act-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@
// https://github.com/testing-library/react-testing-library/blob/3dcd8a9649e25054c0e650d95fca2317b7008576/src/act-compat.js
import * as React from 'react';

import * as DeprecatedReactTestUtils from 'react-dom/test-utils';

declare const globalThis: {
IS_REACT_ACT_ENVIRONMENT: boolean;
};

let reactDomTestUtils;

try {
reactDomTestUtils = require('react-dom/test-utils');
} catch (e) {
reactDomTestUtils = () => ({
act: async (cb: (() => void) | (() => Promise<void>)) => {
await cb();
},
});
}

// @ts-expect-error act might not be available in some versions of React
const reactAct = typeof React.act === 'function' ? React.act : DeprecatedReactTestUtils.act;
const reactAct = typeof React.act === 'function' ? React.act : reactDomTestUtils.act;

export function setReactActEnvironment(isReactActEnvironment: boolean) {
globalThis.IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment;
Expand Down

0 comments on commit 52c264f

Please sign in to comment.