Repository navigation
[test] Fix flaky runtime theme bridge test in WebKit - #49761
Conversation
The bridge test waited with `waitFor` for a state update that React commits in a scheduler task. On CI WebKit, that task can run after the `waitFor` timeout, so the test failed with the text still "base". Use `flushMicrotasks()` to commit the update inside `act()`. The update then does not depend on the scheduler task. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Deploy previewBundle size
PerformanceNo regressions in 32 benchmarks · ran 2m 06s · details Check out the code infra dashboard for more information about this PR. |
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the test-only change to React update flushing.
No open issues 🎉
What changed in this PR
This PR addresses a flaky docs runtime theme bridge test in WebKit by flushing React updates inside act() rather than relying on scheduler timing. Production code is unchanged.
Changes:
- Replace the first theme assertion’s
waitForwithflushMicrotasks()followed by a direct assertion. - Add a comment explaining why delayed scheduler tasks can cause CI timeouts.
| File | Description |
|---|---|
packages-internal/core-docs/src/DemoThemeProviders/DemoThemeProviders.test.tsx |
Flush pending React updates before asserting the injected theme, while preserving cache and runtime checks. |
Review effort: Balanced
|
Confirmed the before-and-after behavior in local WebKit by injecting a 10-second delay into React Scheduler’s Test:
The pre-fix failure matched the reported CI error: The harness intercepted only messages targeting Scheduler’s The runs used the exact before/after test source without modifying tracked files. This reproduces the reported failure mechanism and confirms that the fix avoids it; it does not establish why the CI browser originally stalled. |
The test
docs demo theming > loads and caches the runtime theme bridge after exposing React and jsxfails intermittently in the CircleCItest_browserjob, step "Tests WebKit":expected div[data-testid="first"] to have text 'injected', but the text was 'base'. Examples: https://app.circleci.com/jobs/github/mui/material-ui/991736 (#49747) and https://app.circleci.com/jobs/github/mui/material-ui/992246 (#49696).Root cause:
react/jsx-runtimeimport is not the cause. [core-docs] Import react/jsx-runtime statically for the runtime theme bridge #49658 replaced it with a static import, and build 991736 already included that change.setRuntimeThemeruns outsideact(). React commits that update in a scheduler task (aMessageChanneltask in browsers).waitFortimeout is 1000ms. The CI WebKit page stalled, and the scheduler task did not run before the timeout.MessageChannelmessages gives the same error. Without the delay, the failure does not occur on macOS WebKit.Fix: use
flushMicrotasks()(await act(async () => {})) and assert directly. React then commits the update insideact(), without the scheduler task. With the scheduler channel blocked for 10s, the test passes in about 13ms.Validation:
The core-docs browser project passes 10 times in a row in WebKit and in Chromium with
--no-isolate --no-file-parallelism.The full browser suite passes in WebKit and Chromium. The jsdom run passes.
I have followed (at least) the PR section of the contributing guide.
Generated with Claude Code