Skip to content

test(unit): wait for the async call instead of a single macrotask tick - #576

Merged
rubenvdlinde merged 1 commit into
developmentfrom
fix/unit-flake-single-tick-flush
Aug 31, 2026
Merged

test(unit): wait for the async call instead of a single macrotask tick#576
rubenvdlinde merged 1 commit into
developmentfrom
fix/unit-flake-single-tick-flush

Conversation

@rubenvdlinde

Copy link
Copy Markdown
Contributor

SecretRequestFill > encrypts on submit and shows the success message failed once in roughly twenty full-suite runs with expected "Mock" to be called at least once, and passed 12 out of 12 when run on its own. That split is the tell: the failure needs a loaded suite, not a broken component.

Cause

The helper these specs share yields exactly one macrotask tick:

const flush = () => new Promise((resolve) => setTimeout(resolve, 0))

The submit path awaits an async encryption chain before it posts, and one tick is not guaranteed to drain it. Under parallel workers competing for CPU the assertion can run before the call lands, so the test reports a component that never posted when it simply had not posted yet.

Five sites share the shape await flush() followed immediately by expect(<mock>).toHaveBeenCalled(). All five now wait for the call:

await vi.waitFor(() => expect(post).toHaveBeenCalled())

This is strictly stronger than the fixed tick: it retries until the call arrives, and still fails when it never does.

The negative control, including the one that proved nothing

A retrying assertion that cannot fail would be worse than the flake, so I checked rather than assumed. Removing the submit trigger from the component makes the test fail (exit 1), so the assertion still has teeth.

The first attempt at that control is worth recording because it looked like it passed cleanly. Renaming the event to submit.prevent-DISABLED still fires a submit, since Vue Test Utils reads everything after the dot as modifiers. The sabotage was inert and the suite went green, which I briefly read as evidence about the test rather than about my control.

flush() stays for the many uses that only need a render tick.

Also

15 pre-existing import-order errors in the same five files. They are not gating, since npm run lint covers src only.

Verified

  • unit suite 678/678 across four full runs
  • eslint clean on the changed files
  • prettier clean

SecretRequestFill "encrypts on submit and shows the success message"
failed once in roughly twenty full-suite runs with

  AssertionError: expected "Mock" to be called at least once

and passed 12 out of 12 times when the file was run on its own. That
split is the tell: the failure needs a loaded suite, not a broken
component.

The helper these specs share is

  const flush = () => new Promise((resolve) => setTimeout(resolve, 0))

which yields exactly ONE macrotask tick. The submit path awaits an async
encryption chain before it posts, and one tick is not guaranteed to drain
it. Under parallel workers competing for CPU the assertion can run before
the call lands, so the test reports a component that never posted when in
fact it had not posted YET.

Five sites share that shape: `await flush()` followed immediately by
`expect(<mock>).toHaveBeenCalled()`. All five now wait for the call:

  await vi.waitFor(() => expect(post).toHaveBeenCalled())

This is strictly stronger than the fixed tick. It retries until the call
arrives and still fails when it never does, which was verified rather
than assumed: removing the submit trigger from the component makes the
test fail (exit 1). The first attempt at that control was itself useless
and is worth recording, because it looked like it worked. Renaming the
event to `submit.prevent-DISABLED` still fires a submit, since Vue Test
Utils reads everything after the dot as modifiers, so the sabotage was
inert and the suite passed. A negative control that does not actually
break the thing proves nothing.

flush() is left in place for the many uses that only need a render tick.

Also fixes 15 pre-existing import-order errors in the same five files.
They are not gating: `npm run lint` covers `src` only.

Verified: unit suite 678/678 on four full runs, eslint clean on the
changed files, prettier clean.
@github-actions

Copy link
Copy Markdown
Contributor

Quality Report — ConductionNL/keepiq @ 964da11

Check PHP Vue Security License Tests
lint
phpcs
phpmd
psalm
phpstan
phpmetrics
eslint
stylelint
build
check-manifest
test-l10n
format
check-l10n-js
check-schema-l10n
composer ✅ 111/111
npm ✅ 536/536
app:check-code ⏭️
info.xml
REUSE
PHPUnit
Newman
Playwright ⏭️ deferred — runs on the promotion into beta/main, not on a pull request into development
Hydra gates

Quality workflow — 2026-08-31 16:29 UTC

Download the full PDF report from the workflow artifacts.

@rubenvdlinde
rubenvdlinde merged commit 78b88be into development Aug 31, 2026
49 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.

1 participant