Skip to content

test: fix flaky tests - #10726

Merged
lizschwab merged 2 commits into
RaspberryPiFoundation:mainfrom
microbit-matt-hillsdon:fix/flaky-tests-focus-reset
Nov 30, 2026
Merged

lizschwab merged 2 commits into
RaspberryPiFoundation:mainfrom
microbit-matt-hillsdon:fix/flaky-tests-focus-reset

Conversation

@microbit-matt-hillsdon

Copy link
Copy Markdown
Collaborator

The basics

The details

Resolves

Fixes #10724 (hopefully, draft pending CI pass, if it passes then a re-run would be nice to confirm)

Analysis

After hitting the flakes in the PR tests for #10723 I had an LLM review recent CI failures and hammer the tests under load. The investigation was LLM supported and the fixes were LLM generated. I've reviewed the logic of the failure modes and the fixes.

Recent failures in CI (LLM-generated table from GitHub data):

PR Run Job Failing tests
#10707 feat: support minimum contrast ratios for block text 45249423946, 23 Nov Node 22.x Mutator › Firing change event › XML; Render Management › records undo event when the render was queued with recordUndo enabled; Workspace comment › Events › delete, resize
#10718 test: Convert some more tests to Typescript 45266825020, 23 Nov Node 22.x Mutator › Firing change event › JSO
#10723 fix: close the event group when a keyboard move ends 45657485695, 25 Nov Node 24.x Mutator › XML and JSO; both Render Management bump tests; Workspace comment › Events › collapse, delete, move, resize
#10723, after the push 45661267927, attempt 1 Node 22.x Mutator › XML; Workspace comment › delete. Someone re-ran it, and attempt 2 passed.

All of these runs came after #10712 (jsdom 30.1.3 to 31.0.1) merged on 23 November, which relates to one of the fixes.

I can reproduce the flakes locally on main: they failed in 6 of 12 full runs with 3 workers and the CPU loaded.

Further investigation found the workspace comment tests failed every time when focus_manager_test.js, or any test that focuses the body, ran first in the same process (build first). This one's easy to repro:

npx mocha --config tests/mocha/.mocharc.node.cjs --no-parallel \
  tests/mocha/focus_manager_test.js \
  tests/mocha/rendered_workspace_comment_test.ts

The procedures tests for parameter rename/delete/reorder also failed on their own about a quarter of the time. I didn't see this in CI.

Proposed Changes

There are two separate issues contributing to the flakes I see. I've done one PR with two commits to see clean builds in CI but happy to split this for merge if desired.

The procedures test flakes I saw locally show up a minor async bug which I've fixed in c117135. Although a real bug, I expect in practice it only shows up in the tests as it's very timing dependent.

The other issue relates to how jsdom schedules animation frames. It uses setInterval, which sharedTestSetup/sinon swaps out for fakes. The real setInterval is later restored in sharedTestTeardown. jsdom's rendering interval uses whatever setInterval is current when it starts and sticks with it until idle.

The teardown in focus_manager_test.js focuses the body; this carries through into subsequent tests. Since jsdom 31 (jsdom/jsdom#4434), any attribute or style change while an element has focus starts the rendering interval to do focus fixup (checking the current element is still focusable). Some of these changes happen after the real timers have been restored (e.g. teardown removing the widget div).

Blockly fires events after an animation frame and leans on clock.runAll() to make sure they're seen by tests. But if the frame runs on the real interval then runAll won't run them, so tests miss events non-deterministically.

The smallest fix is to reset focus systematically, before restoring timers. I've taken that approach in 8e80821.

An alternative, that might be more robust to badly behaved tests, is to replace jsdom's RAF implementation in node-setup.mjs, e.g.

window.requestAnimationFrame = (callback) =>
  setTimeout(() => callback(window.performance.now()), 1000 / 60);
window.cancelAnimationFrame = (handle) => clearTimeout(handle);

As this always delegates to setTimeout it can be replaced as needed by the tests. But this is a more invasive change.

MutatorIcon and WarningIcon.setBubbleVisible wait for queued renders
before opening or closing the bubble. Callers such as undoing a mutation
change don't await it, so the block can be disposed in the meantime. The
icon then built a bubble for the disposed block, or disposed the
already-disposed mini workspace and fired a bubble_open event for it,
which threw errors.

Return after waiting if the block has been disposed; dispose() has
already cleaned up the bubble. CommentIcon already does this via its
rendered check.

In the Node test run, the stray events were queued after teardown with
their flush scheduled on a restored clock, so later procedure tests saw
no events and failed intermittently.

Part of RaspberryPiFoundation#10724
JSDom runs all of a window's animation frames from one setInterval,
created with whatever setInterval is global at the time and reused until
no frames are pending. Since jsdom 31, while any element has focus,
attribute and style changes also start that interval, to check the
element is still focusable (jsdom/jsdom#4434).

focus_manager_test.js focused the body after sharedTestTeardown had
restored the real timers, and nothing reset that, so every later test in
the worker started the interval when its teardown changed the DOM after
the restore. Later tests' frames then ran on a real interval their fake
clock couldn't control. Blockly fires events after a frame, so those
tests saw events late or not at all, depending on load and which files
shared a worker.

Clear focus in sharedTestTeardown while the fake clock is still
installed, and drop the body focus from focus_manager_test.js's
teardown, which the blur now covers.

Part of RaspberryPiFoundation#10724
@microbit-matt-hillsdon

microbit-matt-hillsdon commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

I guess the best fix for the PR title lint is probably to split this, but another green run with both together would be good to see first.

@gonfunko

Copy link
Copy Markdown
Contributor

Thanks @microbit-matt-hillsdon ! I was also poking at this last week, but I like this approach better. I think this should be good to merge if you want to un-draft it.

@microbit-matt-hillsdon microbit-matt-hillsdon changed the title Fix flaky tests test: fix flaky tests Nov 30, 2026
@microbit-matt-hillsdon
microbit-matt-hillsdon marked this pull request as ready for review November 30, 2026 17:31
@microbit-matt-hillsdon
microbit-matt-hillsdon requested a review from a team as a code owner November 30, 2026 17:31

@lizschwab lizschwab 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.

Looks good. I pulled down the changes and tested them locally and everything looks good. Thanks for taking this on!

@lizschwab
lizschwab merged commit c73369d into RaspberryPiFoundation:main Nov 30, 2026
12 of 13 checks passed
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.

Investigate flaky tests

3 participants