fix(core): count a negation's hold only up to the timeout after the last sleep

Merged
OK
3d ago
+30-1

What

A negated assertion such as expect(locator).not.toBeVisible() now counts the time the negation held only up to its timeout on every path. Before, a last poll timer that fired late added time past the timeout to the hold, which could turn a too-short hold into a pass. Load can still delay the first read that sees the negation; that is unchanged.

Why

The negation has to hold for 1 s (NEGATION_GRACE_MS) before it passes. Say it is first seen at 2002 ms of a 3000 ms timeout and the last read before the deadline is at 2902 ms. The poll sleeps the remaining ~98 ms and checks the hold once more. If that timer fires on time, the hold is 998 ms and the assertion fails. On a loaded machine the timer can fire at 3053 ms, which gives a hold of 1051 ms, so the assertion passes. The same test flips between pass and fail with machine load.

How it works

  1. After the final sleep, the negated check clamps the read time to the deadline: holds(Math.min(readAt, deadline.endsAt)) (packages/core/src/internal/time.ts:283). The two other hold checks in the loop (lines 269 and 278) already clamp this way. This one was missed.

Verified

Ran it locally: yes

  • Regression test packages/e2e/tests/unit/time.test.ts ("counts a negation as held only until the deadline when the sleep before the last read wakes late"). It uses fake timers: the condition flips false at 2050 ms, the last read is at 2900 ms, and the clock jumps 150 ms during the final sleep. It fails without the fix (AssertionError: promise resolved "undefined" instead of rejecting) and passes with it.
  • Real timers, using the audit's probe (pollCondition, 3000 ms timeout, negation from ~2000 ms, optional 150 ms event-loop block during the final sleep), run against packages/core/src on both checkouts:
lagmainthis PR
0 ms (timer fired 2 ms late)passed at 3002 msfailed at 3002 ms
150 mspassed at 3057 msfailed at 3054 ms
  • Behaviour otherwise unchanged: when the timer fires on time, readAt equals endsAt, so the clamp does nothing. Positive polls and the cut-off read path are untouched. Existing time.test.ts cases, the full e2e unit suite and the @e2e-dev/core tests pass unmodified.
  • Ran it locally through the CLI: no. The bug needs a timer to fire late at the deadline, which a testbed run cannot trigger on demand.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md).
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.