Skip to content

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

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

kevinccbsg merged 1 commit into
feat/mock-rule-ackfrom
fix/beta-timeouts-and-hooks

Conversation

@kevinccbsg

Copy link
Copy Markdown
Member

Feedback from running 1.12.0-beta.0 on two real apps (322 and 550 tests). Both are green in CI, and the larger one runs ~20% faster. A local run with a 2x CPU throttle and no retries found 7 first-attempt failures. 4 were badly written tests the old implicit waits hid. 3 were twd-js limits too short for a 2-vCPU runner. This PR fixes those 3 and a runner bug found along the way.

1. Default timeouts: 3000ms, like findBy*

Wait Before Now
findBy* / twd.waitFor 3000ms 3000ms
twd.waitForRequest 1000ms (10 × 100ms) 3000ms (30 × 100ms)
twd.url().should() 500ms (5 × 100ms) 3000ms (30 × 100ms)

Passing tests are not slower: only a failing one takes longer to be reported. Explicit retries / retryDelay are respected as before.

2. A throwing afterEach ended the whole run

afterEach hooks ran in a finally outside the try, so an error in one escaped runTest, rejected runAll, and twd-cli stopped with "Run interrupted before completion" (seen at 350/550). Also, a passing test had already been reported as passed before its afterEach ran.

Now the hooks run in their own try, and the result is reported after them:

  • A throwing afterEach fails that test, is retried like any failure, and the run continues.
  • When both the test and its afterEach throw, the test's error is the one reported.
  • beforeEach was already fine: it runs inside the try.

3. Docs

  • waitForRequest guarantees the request happened, not that the UI has updated. It resolves on the hit plus two frames, and an app that processes the response asynchronously renders later. Wait for the UI with findBy* / twd.waitFor (example added). The JSDoc said otherwise too, fixed.
  • findBy* returns an element that already existed: put the expected state in the query (findByRole("button", { name: "2", current: "page" })).
  • getRequestCount can lag one hit behind the UI: check it inside twd.waitFor.
  • New defaults in the API reference, url().should() reference and tutorial. component-testing mentions the new afterEach behavior.

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

  • Runner: a throwing afterEach fails the test and the next tests still run; a passing test with a throwing afterEach is not reported as passed; the test's own error wins; a flaky afterEach is retried.
  • waitForRequest rejects at 3000ms by default; waitForRequests reports 3000ms.
  • url().should() passes when the navigation lands after 2000ms and gives up after 3000ms by default.

examples/twd-test-app e2e: 55/55 in 3 of 3 runs, 10.8s (unchanged, as expected).

…terEach no longer ends the run

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.
@kevinccbsg
kevinccbsg merged commit 5d88af5 into feat/mock-rule-ack Oct 18, 2026
1 check passed
@kevinccbsg
kevinccbsg deleted the fix/beta-timeouts-and-hooks branch October 18, 2026 22:17
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