-
Notifications
You must be signed in to change notification settings - Fork 337
Fix React act environment setup in vitest browser tests #775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
sunil: Since we were using plain react act() calls, I had to add a setup file for vitest that enabled the IS_REACT_ACT_ENVIRONMENT flag. Then I discovered that vitest-browser-react@1 has a bug where it would set the global IS_REACT_ACT_ENVIRONMENT to false after a run of it's own render (which wraps act()), so if you ran react's own act() after it, you'd get the "The current testing environment is not configured to support act(...)" error. After fixing that, react correctly identified some tests had mutations that weren't wrapped with act, so I fixed them with a small sleep helper. copilot: Adds a setup file to properly set the IS_REACT_ACT_ENVIRONMENT global for React tests, updates vitest config to use this setup, and patches vitest-browser-react to restore the environment variable after act calls. Also refactors tests to use async act and a sleep helper for more reliable async behavior.
|
Claude Code ReviewIssues:
Minor:
The core fix (preserving |
commit: |
| import type { useAgent } from "agents/react"; | ||
|
|
||
| function sleep(ms: number) { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ps: node:timers/promises will actieve the same thing
await setTimeout(number)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nerrrrd
sunil: Since we were using plain react act() calls, I had to add a setup file for vitest that enabled the IS_REACT_ACT_ENVIRONMENT flag. Then I discovered that vitest-browser-react@1 has a bug where it would set the global IS_REACT_ACT_ENVIRONMENT to false after a run of it's own render (which wraps act()), so if you ran react's own act() after it, you'd get the "The current testing environment is not configured to support act(...)" error. After fixing that, react correctly identified some tests had mutations that weren't wrapped with act, so I fixed them with a small sleep helper.
copilot: Adds a setup file to properly set the IS_REACT_ACT_ENVIRONMENT global for React tests, updates vitest config to use this setup, and patches vitest-browser-react to restore the environment variable after act calls. Also refactors tests to use async act and a sleep helper for more reliable async behavior.