question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The current testing environment is not configured to support act(...) with vitest and React 18

See original GitHub issue

@testing-library/jest-dom”: “5.16.4”, “@testing-library/react”: “13.1.1”, DOM Environment: jsdom or happy-dom (happens with both)

Problem description:

After upgrading to @testing-library/react@13.x and react@18.x with vitest. A lot of react tests throw the following warning:

Warning: The current testing environment is not configured to support act(...)

I noticed this after switching from jest to vitest, and not changing anything else. (The tests do not show this warning when using jest, but they they show it when using vitest in the same project).

I previously had an unrelated issue causing the same warning with Jest: https://github.com/testing-library/react-testing-library/issues/1057

I resolved that, by removing a bad implementation of act(). However this time the tests are identical, and works with 1 test-runner and not with another.

Reproduction:

Credit to @stuarthallows for creating a repo that reproduces the problem: https://github.com/stuarthallows/vitest-error

@stuarthallows and @dar5hak also both seem to be having the same problem as me (see my other issue here: https://github.com/testing-library/react-testing-library/issues/1057 for more details).

Additional information.

In my case, this is breaking unit tests completely because it logs thousands of these warnings and it just stops after around 10 tests are completed.

For me a lot of the errors start with rc-motion

Warning: The current testing environment is not configured to support act(...)
    at Align (/Users/marklyck/colony/colony-frontend/node_modules/.pnpm/rc-align@4.0.12_ef5jwxihqo6n7gxfmzogljlgcm/node_modules/rc-align/lib/Align.js:45:23)
    at DomWrapper (/Users/marklyck/colony/colony-frontend/node_modules/.pnpm/rc-motion@2.5.1_ef5jwxihqo6n7gxfmzogljlgcm/node_modules/rc-motion/lib/DomWrapper.js:28:34)
    at /Users/marklyck/colony/colony-frontend/node_modules/.pnpm/rc-motion@2.5.1_ef5jwxihqo6n7gxfmzogljlgcm/node_modules/rc-motion/lib/CSSMotion.js:57:32

Which is probably running an animation with antd components. But I’m 99% sure that’s a red-herring since the same tests and components work just fine with jest. (or testing-library/react@12 and react@17)

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:19
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
airjp73commented, May 5, 2022

The workaround doesn’t totally work for me. The initial error The current testing environment is not configured... to support act(...) goes away, but then I get act warnings I wouldn’t have otherwise.

I’ve made a reproduction here: https://github.com/airjp73/vitest-act-reproduction

Worth noting that this also appears to be exclusive to React 18. If you downgrade everything to React 17 (including downgrading this library to v12), the act warnings go away and you also don’t need to override the global anymore.

6reactions
nknappcommented, May 6, 2022

I raised a similar issue at vitest. But they think (and I kind of agree), that it has to be solved in testing-library.

tldr; of my vitest-issue:

  • when testing-library/react resolves IS_REACT_ACT_ENVIRONMENT it uses the first existing of self, window and global as global-object, which in may case is self
  • react access IS_REACT_ACT_ENVIRONMENT directly, which is probably globalThis or global link.
  • in vitest / jsdom, globalThis and self are not the same object.

The fix

I think the fix is to add

if (typeof globalThis !== 'undefined') {
  return globalThis
}

here: https://github.com/testing-library/react-testing-library/blob/c8c93f83228a68a270583c139972e79b1812b7d3/src/act-compat.js#L5-L28.

The workaround

The workaround is define some kind of proxy-property on global this, that will make sure that self and globalThis have the same value of IS_REACT_ACT_ENVIRONMENT the code in setupFile.ts:

Object.defineProperty(globalThis,"IS_REACT_ACT_ENVIRONMENT", {
  get() {
    if (typeof globalThis.self !== 'undefined') {
      return globalThis.self.IS_REACT_ACT_ENVIRONMENT
    }
  },
  set(value) {
    if (typeof globalThis.self !== 'undefined') {
      globalThis.self.IS_REACT_ACT_ENVIRONMENT = value
    }
  }
})

globalThis.IS_REACT_ACT_ENVIRONMENT = true
Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - The current testing environment is not configured ...
In my case this happened because I had a useless act that I implemented as a workaround in v12. await act(async () =>...
Read more >
Setup | Testing Library
React Testing Library does not require any configuration to be used. However, there are some things you can do when configuring your testing ......
Read more >
react-testing-library
Simple and complete React DOM testing utilities that encourage good testing practices ... The current testing environment is not configured to support act(....
Read more >
React Testing Library and the “not wrapped in act” Errors
I recently upgraded React and React testing library. Very happy about the upgrade. But I start to see test errors like this: In...
Read more >
Vitest with React Testing Library
A brief walkthrough on how to set up React Testing Library in Vitest when using Vite. The tutorial assumes that you have already...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found