From 52c264f18f142762d84a7219c19e2ea2fcd48448 Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Thu, 5 Sep 2024 15:33:36 +0200 Subject: [PATCH] Next.js: Fix react-dom/test-utils aliasing --- code/frameworks/nextjs/src/config/webpack.ts | 2 +- code/renderers/react/src/act-compat.ts | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/code/frameworks/nextjs/src/config/webpack.ts b/code/frameworks/nextjs/src/config/webpack.ts index 3e0d758c1514..6dbb94280c0d 100644 --- a/code/frameworks/nextjs/src/config/webpack.ts +++ b/code/frameworks/nextjs/src/config/webpack.ts @@ -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); diff --git a/code/renderers/react/src/act-compat.ts b/code/renderers/react/src/act-compat.ts index afe1cc902316..3dda4c0f64fb 100644 --- a/code/renderers/react/src/act-compat.ts +++ b/code/renderers/react/src/act-compat.ts @@ -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)) => { + 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;