Skip to content

Conversation

@threepointone
Copy link
Contributor

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.

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.
@changeset-bot
Copy link

changeset-bot bot commented Jan 12, 2026

⚠️ No Changeset found

Latest commit: 0b4e75c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@claude
Copy link

claude bot commented Jan 12, 2026

Claude Code Review

Issues:

  1. Hardcoded sleep timing (packages/ai-chat/src/react-tests/use-agent-chat.test.tsx:13-15): The sleep(10) calls use an arbitrary 10ms delay. This creates brittle tests that could fail on slower CI runners or pass incorrectly on fast machines. Consider using proper async patterns like waitFor from testing-library or event-based synchronization instead of arbitrary timeouts.

  2. Inconsistent async test pattern (multiple locations): Some tests mix await sleep(10) after render() while one test (around line 487-507) had a more complex pattern with an additional 100ms wait that was removed. The removal of that wait suggests timing dependencies that should be addressed with proper synchronization primitives rather than sleeps.

  3. Patch maintenance burden (patches/vitest-browser-react+1.0.1.patch): The patch fixes a legitimate bug in vitest-browser-react, but patching node_modules creates long-term maintenance issues:

    • Requires re-applying on every package update
    • May break silently if the library's internal structure changes
    • Consider opening an upstream PR to vitest-browser-react to fix this properly
  4. Missing changeset: The PR doesn't include a changeset file for @cloudflare/ai-chat to document this test infrastructure improvement for changelog generation.

Minor:

  • The patch file comment at line 9 has a trailing space

The core fix (preserving IS_REACT_ACT_ENVIRONMENT) is correct and addresses the immediate issue, but the sleep-based synchronization approach is fragile.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 12, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@775

commit: 0b4e75c

@threepointone threepointone merged commit 628bf8c into main Jan 12, 2026
5 checks passed
@threepointone threepointone deleted the fix-react-act-tests branch January 12, 2026 16:37
import type { useAgent } from "agents/react";

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
Copy link
Member

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nerrrrd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants