Skip to content

[test] Fix flaky runtime theme bridge test in WebKit - #49761

Merged
LukasTy merged 1 commit into
mui:masterfrom
LukasTy:fix-flaky-demo-theme-bridge-test
Nov 11, 2026
Merged

LukasTy merged 1 commit into
mui:masterfrom
LukasTy:fix-flaky-demo-theme-bridge-test

Conversation

@LukasTy

@LukasTy LukasTy commented Nov 11, 2026

Copy link
Copy Markdown
Member

The test docs demo theming > loads and caches the runtime theme bridge after exposing React and jsx fails intermittently in the CircleCI test_browser job, 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:

  • The dynamic react/jsx-runtime import 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.
  • The bridge promise resolves in one microtask, but setRuntimeTheme runs outside act(). React commits that update in a scheduler task (a MessageChannel task in browsers).
  • Both failures took about 2s, but the waitFor timeout is 1000ms. The CI WebKit page stalled, and the scheduler task did not run before the timeout.
  • Locally, a delay on the scheduler's MessageChannel messages 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 inside act(), 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

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>
@LukasTy LukasTy changed the title [test][docs-infra] Fix flaky runtime theme bridge test in WebKit [test] Fix flaky runtime theme bridge test in WebKit Nov 11, 2026
@LukasTy LukasTy added type: bug It doesn't behave as expected. test labels Nov 11, 2026
@LukasTy
LukasTy requested a balanced review from Copilot November 11, 2026 13:05
@code-infra-dashboard

code-infra-dashboard Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-49761--material-ui.netlify.app/
QR code for https://deploy-preview-49761--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 0B(0.00%) 0B(0.00%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Performance

No regressions in 32 benchmarks · ran 2m 06s · details


Check out the code infra dashboard for more information about this PR.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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 waitFor with flushMicrotasks() 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

@LukasTy
LukasTy marked this pull request as ready for review November 11, 2026 13:24

LukasTy commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

Confirmed the before-and-after behavior in local WebKit by injecting a 10-second delay into React Scheduler’s MessageChannel messages, while leaving async act() messages and the normal waitFor timeout unchanged.

Test: docs demo theming > loads and caches the runtime theme bridge after exposing React and jsx, run with --no-isolate --no-file-parallelism.

Test version Scheduler delay Result Test duration
Before fix (bf5e8eb64c) None Passed 32 ms
Before fix (bf5e8eb64c) 10 seconds Failed 1,046 ms
After fix (f061251d2e) 10 seconds Passed 29 ms

The pre-fix failure matched the reported CI error:

expected div[data-testid="first"] to have text 'injected', but the text was 'base'

The harness intercepted only messages targeting Scheduler’s performWorkUntilDeadline callback. In the failing run, one scheduler message was held beyond the assertion timeout. With the fix, the test posted no scheduler messages and completed through an undelayed async act() channel.

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.

@LukasTy
LukasTy merged commit 66c9957 into mui:master Nov 11, 2026
21 of 22 checks passed
@LukasTy
LukasTy deleted the fix-flaky-demo-theme-bridge-test branch November 11, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants