File tree Expand file tree Collapse file tree 3 files changed +15
-9
lines changed Expand file tree Collapse file tree 3 files changed +15
-9
lines changed Original file line number Diff line number Diff line change 22// https://github.com/testing-library/react-testing-library/blob/3dcd8a9649e25054c0e650d95fca2317b7008576/src/act-compat.js
33import * as React from 'react' ;
44
5- import * as DeprecatedReactTestUtils from 'react-dom/test-utils' ;
6-
75declare const globalThis : {
86 IS_REACT_ACT_ENVIRONMENT : boolean ;
97} ;
@@ -13,9 +11,6 @@ declare const globalThis: {
1311// We do check if act exists, but webpack will still throw an error on compile time
1412const clonedReact = { ...React } ;
1513
16- const reactAct =
17- typeof clonedReact . act === 'function' ? clonedReact . act : DeprecatedReactTestUtils . act ;
18-
1914export function setReactActEnvironment ( isReactActEnvironment : boolean ) {
2015 globalThis . IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment ;
2116}
@@ -67,7 +62,15 @@ function withGlobalActEnvironment(actImplementation: (callback: () => void) => P
6762 } ;
6863}
6964
70- export const act =
71- process . env . NODE_ENV === 'production'
65+ export const getAct = async ( ) => {
66+ // Lazy loading this makes sure that @storybook /react can be loaded in SSR contexts
67+ // For example when SSR'ing portable stories
68+ const reactAct =
69+ typeof clonedReact . act === 'function'
70+ ? clonedReact . act
71+ : ( await import ( 'react-dom/test-utils' ) ) . act ;
72+
73+ return process . env . NODE_ENV === 'production'
7274 ? ( cb : ( ...args : any [ ] ) => any ) => cb ( )
7375 : withGlobalActEnvironment ( reactAct ) ;
76+ } ;
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import * as React from 'react';
22
33import semver from 'semver' ;
44
5- import { act , getReactActEnvironment , setReactActEnvironment } from './act-compat' ;
5+ import { getAct , getReactActEnvironment , setReactActEnvironment } from './act-compat' ;
66import type { Decorator } from './public-types' ;
77
88export const parameters = { renderer : 'react' } ;
@@ -32,6 +32,8 @@ export const beforeAll = async () => {
3232 // https://github.com/testing-library/react-testing-library/blob/3dcd8a9649e25054c0e650d95fca2317b7008576/src/pure.js
3333 const { configure } = await import ( '@storybook/test' ) ;
3434
35+ const act = await getAct ( ) ;
36+
3537 configure ( {
3638 unstable_advanceTimersWrapper : ( cb ) => {
3739 return act ( cb ) ;
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import type { RenderContext } from 'storybook/internal/types';
55
66import { global } from '@storybook/global' ;
77
8- import { act } from './act-compat' ;
8+ import { getAct } from './act-compat' ;
99import type { ReactRenderer , StoryContext } from './types' ;
1010
1111const { FRAMEWORK_OPTIONS } = global ;
@@ -98,6 +98,7 @@ export async function renderToCanvas(
9898 unmountElement ( canvasElement ) ;
9999 }
100100
101+ const act = await getAct ( ) ;
101102 await new Promise < void > ( async ( resolve , reject ) => {
102103 actQueue . push ( async ( ) => {
103104 try {
You can’t perform that action at this time.
0 commit comments