fix(expect): unawaited poll fails its own phase, slow first read counts toward negation window

Merged
OK
2mo ago
+784-84

Two timing bugs in assertion polling.

Unawaited expect.poll (B58)

  • expect.poll(...).toBe(...) returned a promise the attempt never knew about. Without await, the test passed (exit 0 when it was the last test), and the poll's timeout rejected later. strayRejection then charged it to whichever test was running at that moment, or recorded it as UNHANDLED_REJECTION after 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 and beforeEach), each afterEach hook or fixture teardown, and each beforeAll/afterAll hook. Ownership follows the async lineage (AsyncLocalStorage, shared through a globalThis slot so a test module's copy of expect reads 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_AWAITED at 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 under secondaryErrors. A suite hook reports it as HOOK_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 use is released. An awaited poll after use still works, and an unawaited one fails as fixture "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 from steps.ts to internal/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) computed grace = min(1000, remaining) before the first read, but started the "negation holding since" clock only after evaluate() 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 with observed: 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.

Scenariomainthis 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 testpoll test ✓, innocent test × "timed out after 300 ms"poll test × STEP_NOT_AWAITED, innocent test ✓
unawaited poll in afterEach / beforeEach / afterAll, then innocent testowners ✓, innocent test ×, plus UNHANDLED_REJECTION blaming itafterEach 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 mssame
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's time.ts.
  • tests/unit/expect-poll.test.ts: scope close returns STEP_NOT_AWAITED at 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 no UNHANDLED_REJECTION appears. 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 every expect.poll" sentence followed the Goals section into docs/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: waitFor detached/hidden (300 ms removal), and absent-frame not.toBeVisible({ timeout: 500 }) (503 ms, main 664 ms), not.toBeAttached, toBeHidden, waitFor pass 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.

Devin Review