fix(expect): unawaited poll fails its own phase, slow first read counts toward negation window
Merged
OK
2mo ago+784+784+012345678901234567890123456789-84-84-01234567890123456789
Two timing bugs in assertion polling.
Unawaited expect.poll (B58)
expect.poll(...).toBe(...)returned a promise the attempt never knew about. Withoutawait, the test passed (exit 0 when it was the last test), and the poll's timeout rejected later.strayRejectionthen charged it to whichever test was running at that moment, or recorded it asUNHANDLED_REJECTIONafter the last test.- Each phase now owns the polls it starts through a
PollScope(packages/e2e/src/expect/poll-scope.ts). The phases are the body (with fixtures andbeforeEach), eachafterEachhook or fixture teardown, and eachbeforeAll/afterAllhook. Ownership follows the async lineage (AsyncLocalStorage, shared through aglobalThisslot so a test module's copy ofexpectreads the runner's context). A poll belongs to the phase that started it, not to whatever phase is running when it gets called. - When a phase returns with a poll still running, the poll is cancelled and the phase fails with
STEP_NOT_AWAITEDat the line of the call. This happens whether or not the poll would have passed, and it matches what an unawaited step does. A body that threw keeps its own error and gets this one undersecondaryErrors. A suite hook reports it asHOOK_FAILED. - If work left running by a phase (a body past its timeout) starts a poll after that phase closed, the poll is cancelled before its first read and its rejection is marked abandoned. It never lands on the next test.
- Fixture functions get a lineage that is handed to the teardown's scope when
useis released. An awaited poll afterusestill works, and an unawaited one fails asfixture "x" teardown. - Polls are still not report steps.
runHook(packages/e2e/src/run/realm.ts) gives every hook and teardown its own scope. The abandoned-rejection set moved fromsteps.tstointernal/abandoned.ts, so steps and polls share it.
flowchart LR
P[expect.poll called] --> S{owning scope}
S -- open --> R[runs, tracked]
S -- closed --> C[cancelled before first read]
R --> E{phase returns}
E -- poll settled --> OK[nothing]
E -- still running --> F[cancel + STEP_NOT_AWAITED on that phase]
Negation window with a slow first read (B41)
pollCondition(packages/e2e/src/internal/time.ts) computedgrace = min(1000, remaining)before the first read, but started the "negation holding since" clock only afterevaluate()returned. With a budget under 1 s and a slow first read, a negation that was true the whole time timed out. On a heavy page,expect(screen.getByText('Loading')).not.toBeVisible({ timeout: 500 })failed withobserved: no node.- The window now starts when the read that first saw the negation was issued. A negation that held for the whole budget passes at the deadline on what it has seen, without a read past the deadline, where the engine's operations get a 1 ms budget. The protection the window exists for is unchanged: every pass needs the latest read to show the negation, and a positive read resets the window.
Verified
Ran it locally: yes. Built CLI (node packages/e2e/dist/cli/bin.js run) in scratch projects under /tmp/oskar/fix-poll-drain-negation-timing/ (api target, and a web target on port 5201 serving N rows), with main built from origin/main 3a5651f6.
| Scenario | main | this branch |
|---|---|---|
only test: expect.poll(() => false, { timeout: 100 }).toBe(true) without await (api) | ✓ passed, exit 0 | × STEP_NOT_AWAITED at alone.e2e.ts:3, exit 1 |
| same, web target | ✓ passed, exit 0 | × STEP_NOT_AWAITED, exit 1 |
| unawaited poll (300 ms), then an innocent 600 ms test | poll test ✓, innocent test × "timed out after 300 ms" | poll test × STEP_NOT_AWAITED, innocent test ✓ |
unawaited poll in afterEach / beforeEach / afterAll, then innocent test | owners ✓, innocent test ×, plus UNHANDLED_REJECTION blaming it | afterEach owner × (the afterEach hook returned before...), beforeEach owner ×, HOOK_FAILED (afterAll) naming the hook, innocent test ✓ |
| control: awaited failing poll | × ASSERTION_FAILED timed out after 100 ms | same |
| control: awaited passing poll | ✓ | ✓ |
not.toBeVisible({ timeout: 500 }) on absent text, 3000 rows | × observed: no node | ✓ passed after 511 ms |
| same, 20000 rows (first read ~570 ms) | × | ✓ passed after 567 ms |
control: not.toBeVisible on visible text, 500 ms | × | × |
not.toBeVisible({ timeout: 500 }) on absent text, light page | ✓ | ✓ passed after 502 ms |
Regression tests:
tests/unit/time.test.ts: fake-timer cases for a slow first read (80 ms, then 10 ms) under 500 ms and 1500 ms budgets, fast reads (pass at the deadline, not before, with no read past it), and a positive read inside a short budget (still fails). Three of them fail on main'stime.ts.tests/unit/expect-poll.test.ts: scope close returnsSTEP_NOT_AWAITEDat the call line, cancels the poll (reads stop), and marks the rejection abandoned, derived promises included. Also covered: a poll that would have passed, awaited polls close quietly, a poll started by a closed phase's leftover work is cancelled with zero reads and never joins the running phase, and the fixture lineage hand-over.tests/integration/expect-poll-scope.test.ts: child-process workers covering body, body throw,beforeEach,afterEach, fixture teardown (leaky and awaited),afterAll,beforeAll, and a timed-out body that polls while the next test runs. It checks that no later test is charged and noUNHANDLED_REJECTIONappears. 6 of 9 cases fail on main. The timed-out-body case passes on main too, and guards against a poll joining the phase that is running when it is called.- Rebased onto main 7f9ee108 (after #726, #741, #713, #780). One conflict in
execute.ts: #741 keeps only soft failures when the body skips; kept that, with the not-awaited list now spread (steps and polls). The "await everyexpect.poll" sentence followed the Goals section intodocs/goals.mdx. Rerun on the rebased build vs main 7f9ee108: same results as the table;not.toBeVisible({ timeout: 500 })passed after 501 / 510 / 556 ms at 0 / 3000 / 20000 rows (main fails at 3000 and 20000). With #726:waitFordetached/hidden(300 ms removal), and absent-framenot.toBeVisible({ timeout: 500 })(503 ms, main 664 ms),not.toBeAttached,toBeHidden,waitForpass the same as on main. pnpm check,pnpm --filter e2e run test(3027 passed after rebase),pnpm --filter @e2e-dev/web run test(462 passed), testbed (70 passed), web-benchmark (90 passed): green.