Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Try to use act from 'react'
Browse files Browse the repository at this point in the history
  • Loading branch information
jas7457 committed May 6, 2024
1 parent 7923cc8 commit 8c6546f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-paws-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/react-testing': minor
---

Change act to use from 'react' if it exists, fallback to 'react-dom/test-utils'
8 changes: 6 additions & 2 deletions packages/react-testing/src/root.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import {flushSync} from 'react-dom';
import type {Root as ReactRoot} from 'react-dom/client';
import {act} from 'react-dom/test-utils';
import {act as oldAct} from 'react-dom/test-utils';
import {findCurrentFiberUsingSlowPath} from 'react-reconciler/reflection.js';

import {TestWrapper} from './TestWrapper';
Expand All @@ -22,6 +22,9 @@ import type {
} from './types';
import {Tag} from './types';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const {act} = require('react');

type ResolveRoot = (element: Element<unknown>) => Element<unknown> | null;
type Render = (
element: React.ReactElement<unknown>,
Expand Down Expand Up @@ -109,6 +112,7 @@ export class Root<Props> implements Node<Props> {
}

this.acting = true;
const currentAct = act ?? oldAct;

/* act has two versions, act(() => void) or await act(async () => void)
* The async version will wrap the inner async function so that it maintains an act
Expand All @@ -124,7 +128,7 @@ export class Root<Props> implements Node<Props> {
* check the return value of result to determine if it needs awaiting so that it can
* properly flush updates.
*/
const possiblyAwaitableAct = act(() => {
const possiblyAwaitableAct = currentAct(() => {
flushSync(() => {
result = action();
});
Expand Down

0 comments on commit 8c6546f

Please sign in to comment.