Skip to content

fix(mocking): wait for a busy worker, let the response render after waitForRequest - #401

Merged
kevinccbsg merged 1 commit into
feat/mock-rule-ackfrom
fix/beta-ack-and-settle
Oct 18, 2026
Merged

kevinccbsg merged 1 commit into
feat/mock-rule-ackfrom
fix/beta-ack-and-settle

Conversation

@kevinccbsg

Copy link
Copy Markdown
Member

Fixes for the beta in #398, found by running the example apps against it (twd-test-app plus the Vue, Angular, Solid and React Router examples, with this branch's build and service worker).

1. mockRequest gave up on a busy worker

mockRequest waited 100ms for the ack and then assumed an outdated worker. An up-to-date worker that is busy for a moment (serving the dev server's modules while a page renders) can take longer. mockRequest then carried on before the rule was stored, the app's request reached the worker first and went to the network, and the page printed a wrong "outdated" warning.

Fix: once the worker has acked at least once in this page load, a missing answer means busy, not outdated, so mockRequest waits up to 2000ms. Until the first ack, the 100ms fallback stays, so outdated workers behave exactly as before. A late ack after a timeout still marks the worker as one that acks.

2. waitForRequest resolved before the response rendered

This broke "should delete a todo" in the Angular and Solid examples: re-mock the list, click Delete, await waitForRequest(...), then a synchronous getAllByText(...) found nothing. The worker posts EXECUTED before it returns the mocked response. The old 100ms polling left time for the response by luck, and resolving on the message removed that time.

Fix: after the hit, waitForRequest waits two animation frames (settle() in src/utils/wait.ts, a 16ms timer per frame when the tab is hidden) before resolving, also when the hit happened before the call. That costs ~30ms, against ~50ms on average with the old polling.

Results

Suite main beta before this PR this PR
twd-test-app 55/55, 14.3s 55/55, ~10.5s 55/55 in 5 of 5 runs, ~10.8s
twd-vue-example 7/7 7/7 7/7 (2 runs)
twd-angular-example 4/4 3/4 4/4 (2 runs)
twd-solid-example 4/4 3/4 4/4 (2 runs)
twd-react-router 5/5 5/5 5/5 (2 runs)

A faster twd.visit() (two frames instead of 100ms) was tried in this round and dropped. In twd-test-app it made a test click a button from the screen about to be replaced, in 4 of 5 runs. visit keeps its fixed 100ms.

Tests (written first, each seen failing before the fix)

  • mockRequest: waits for a busy worker that acked before (no warning); learns from a first ack that arrives after the timeout; gives up on a worker that acked before after 2000ms.
  • waitForRequest: a synchronous check right after it sees the rendered response, whether the hit arrives during the wait or before it.

Specs updated with a follow-up section each: specs/2026-10-18-mock-rule-ack-design.md, specs/2026-10-18-wait-for-request-polling-design.md.

…aitForRequest

Two problems found by running the example apps against the beta.

mockRequest gave up on the ack after 100ms and treated the worker as
outdated. An up-to-date worker that is busy for a moment can take longer, so
the app's request reached it before the rule did and went to the network.
Once the worker has acked in this page load, mockRequest now waits up to 2s
for it. Until then the 100ms fallback for outdated workers stays.

waitForRequest resolved on the worker's EXECUTED message, which the worker
sends before it returns the mocked response, so a synchronous query right
after it ran before the response had rendered. It now waits two animation
frames after the hit (a 16ms timer per frame in a hidden tab).
@kevinccbsg
kevinccbsg merged commit 7ef7dfd into feat/mock-rule-ack Oct 18, 2026
1 check passed
@kevinccbsg
kevinccbsg deleted the fix/beta-ack-and-settle branch October 18, 2026 20:23
kevinccbsg added a commit that referenced this pull request Oct 19, 2026
…ms (#398)

* feat(mocking): wait for the service worker ack instead of a fixed 100ms

mockRequest used to sleep 100ms after posting ADD_RULE, on every call, as a
guess at how long the service worker needs to store the rule. It now sends a
MessageChannel port with the message and resolves as soon as the worker
answers on it: about 1ms per mock, and safe under a slow CPU because it waits
for the real confirmation instead of guessing.

Workers from before this change never answer. For them mockRequest resolves
after 100ms, exactly as before, and warns once per page load to run
`npx twd-js init public`. A version mismatch reported by the ack is also
warned once, now in the page console, where twd-cli can surface it.

mock-sw.js is now built unminified with a header naming the version and how
to regenerate it, since users commit and debug this file. The example apps
get the regenerated worker.

* feat(mocking): resolve waitForRequest on the hit, ignore hits on replaced rules (#399)

waitForRequest polled the rules every 100ms, so each call waited ~50ms on
average after the request had already happened. It now resolves from the
worker's EXECUTED message, with the same timeout and error message. A 100ms
backstop check stays for rules marked executed any other way (HMR reloads,
tests that set the flag), so it is never slower than before.

Re-mocking an alias while the first request was still in flight let that late
hit mark the new rule as executed, and the next waitForRequest resolved at
once with the old request. Each mockRequest call now gives its rule an id,
the worker sends it back as ruleId, and the page ignores hits on a replaced
rule. Workers without the id keep the old behaviour.

* fix(url): pass url().should() when a retry passes (#400)

* feat(mocking): resolve waitForRequest on the hit, ignore hits on replaced rules

waitForRequest polled the rules every 100ms, so each call waited ~50ms on
average after the request had already happened. It now resolves from the
worker's EXECUTED message, with the same timeout and error message. A 100ms
backstop check stays for rules marked executed any other way (HMR reloads,
tests that set the flag), so it is never slower than before.

Re-mocking an alias while the first request was still in flight let that late
hit mark the new rule as executed, and the next waitForRequest resolved at
once with the old request. Each mockRequest call now gives its rule an id,
the worker sends it back as ruleId, and the page ignores hits on a replaced
rule. Workers without the id keep the old behaviour.

* fix(url): pass url().should() when a retry passes

should() kept the error from the first failed attempt and threw it even when
a later retry passed, so the retries never helped: any URL assertion that
needed one failed. It now returns as soon as an attempt passes.

The existing retry test did not await the assertion, which hid the bug. It
now awaits it, with a navigation that lands after the first attempt fails.

* fix(mocking): wait for a busy worker, let the response render after waitForRequest (#401)

Two problems found by running the example apps against the beta.

mockRequest gave up on the ack after 100ms and treated the worker as
outdated. An up-to-date worker that is busy for a moment can take longer, so
the app's request reached it before the rule did and went to the network.
Once the worker has acked in this page load, mockRequest now waits up to 2s
for it. Until then the 100ms fallback for outdated workers stays.

waitForRequest resolved on the worker's EXECUTED message, which the worker
sends before it returns the mocked response, so a synchronous query right
after it ran before the response had rendered. It now waits two animation
frames after the hit (a 16ms timer per frame in a hidden tab).

* chore: release 1.12.0-beta.0

* docs: note un-awaited async code in tests as the likely cause of flakiness with 1.12.0-beta.0

* fix: 3s defaults for waitForRequest and url().should(), a throwing afterEach no longer ends the run (#402)

Feedback from running the beta on real apps with a throttled CPU.

waitForRequest waited 1000ms by default and twd.url().should() 500ms, while
findBy* already waits 3000ms. Both were too short for a 2-vCPU CI runner, so
both now default to 3000ms (30 attempts 100ms apart). Passing tests are not
slower, and explicit retries/retryDelay are respected.

An afterEach that threw escaped the test loop and ended the whole run. It now
fails that test (retried like any failure) and the run continues. The result
is reported after the hooks, so a passing test whose afterEach throws is no
longer reported as passed.

Docs: waitForRequest guarantees the request happened, not that the UI has
updated; put the expected state in findBy* queries; getRequestCount can lag
one hit behind the UI.

* chore: release 1.12.0-beta.1

* chore: release 1.12.0
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