Skip to content

Commit

Permalink
Merge pull request #2773 from Shopify/hack-act
Browse files Browse the repository at this point in the history
Use act from 'react' when possible
  • Loading branch information
jas7457 committed May 6, 2024
2 parents 7923cc8 + 8498746 commit 7a0d348
Show file tree
Hide file tree
Showing 3 changed files with 23 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'
11 changes: 11 additions & 0 deletions packages/react-testing/src/compat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ReactDOM from 'react-dom';
import React from 'react';
import type {Root as ReactRoot} from 'react-dom/client';
import {act as oldAct} from 'react-dom/test-utils';

import type {ReactInstance, Fiber} from './types';

Expand Down Expand Up @@ -38,6 +39,16 @@ export function createRoot(element: HTMLElement): ReactRoot {

export const isLegacyReact = parseInt(React.version, 10) < 18;

export const act: typeof oldAct = (() => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {act} = require('react');
return act ?? oldAct;
} catch {
return oldAct;
}
})();

// https://github.com/facebook/react/blob/12adaffef7105e2714f82651ea51936c563fe15c/packages/shared/enqueueTask.js#L13
let enqueueTaskImpl: any = null;

Expand Down
9 changes: 7 additions & 2 deletions packages/react-testing/src/root.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
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 {findCurrentFiberUsingSlowPath} from 'react-reconciler/reflection.js';

import {TestWrapper} from './TestWrapper';
import {Element} from './element';
import {createRoot, getInternals, enqueueTask, isLegacyReact} from './compat';
import {
createRoot,
getInternals,
enqueueTask,
isLegacyReact,
act,
} from './compat';
import type {
Fiber,
Node,
Expand Down

0 comments on commit 7a0d348

Please sign in to comment.