Skip to content

Commit 74512bb

Browse files
committed
Add actionable message for "React.act is not a function" error
1 parent a2d37ff commit 74512bb

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/act-compat.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ function withGlobalActEnvironment(actImplementation) {
7272
return actResult
7373
}
7474
} catch (error) {
75+
// https://github.com/testing-library/react-testing-library/issues/1392
76+
// https://github.com/testing-library/react-testing-library/issues/1399
77+
if (
78+
Error.isError(error) &&
79+
error.toString() === 'TypeError: React.act is not a function' &&
80+
process &&
81+
process.env.NODE_ENV === 'production'
82+
) {
83+
throw new Error(
84+
'Following error may be caused by `NODE_ENV` environment variable being set to `production`. Try changing it to `test`.',
85+
{cause: error},
86+
)
87+
}
7588
// Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT
7689
// or if we have to await the callback first.
7790
setIsReactActEnvironment(previousActEnvironment)

0 commit comments

Comments
 (0)