Repository navigation
fix(mocking): wait for a busy worker, let the response render after waitForRequest - #401
Merged
Merged
Conversation
…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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes for the beta in #398, found by running the example apps against it (
twd-test-appplus the Vue, Angular, Solid and React Router examples, with this branch's build and service worker).1.
mockRequestgave up on a busy workermockRequestwaited 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.mockRequestthen 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
mockRequestwaits 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.
waitForRequestresolved before the response renderedThis broke "should delete a todo" in the Angular and Solid examples: re-mock the list, click Delete,
await waitForRequest(...), then a synchronousgetAllByText(...)found nothing. The worker postsEXECUTEDbefore 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,
waitForRequestwaits two animation frames (settle()insrc/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
maintwd-test-apptwd-vue-exampletwd-angular-exampletwd-solid-exampletwd-react-routerA faster
twd.visit()(two frames instead of 100ms) was tried in this round and dropped. Intwd-test-appit made a test click a button from the screen about to be replaced, in 4 of 5 runs.visitkeeps 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.