feat(expect)!: pass negated matchers on the first read (ENG-1285)

Merged
OK
2d ago
+254-308

What

A negated matcher passes on the first read that satisfies the negation, as in Playwright. It used to pass only after the negation had held for 1 s.

await expect(screen.getByRole('dialog')).not.toBeVisible(); // passes on the first read that sees no dialog
await expect(browser).not.toHaveURL('/login');
  • Covers .not on every locator matcher, on expect(browser) (toHaveURL, toHaveTitle, toHaveClass), on toHaveScreenshot, and pollCondition({ negated: true }) from @e2e-dev/core/engine.
  • A read whose ref another lookup superseded is no longer read as an absent node by toBeHidden(), .not.toBeVisible(), .not.toBeAttached(), and waitFor({ state: 'hidden' | 'detached' }).

Why

Owner decision on 2026-11-13 (ENG-1285): match Playwright. The hold made every passing negated assertion cost 1 s, and it was the last .not row under Planned in the Playwright migration guide.

How it works

The hold lived in one place. Every engine package and matcher polls through pollReads in core; no engine (web, mobile, desktop, or the provider packages) has a window of its own.

  1. pollReads returns when a read answers !negated (packages/core/src/internal/time.ts:257). The grace window, NEGATION_GRACE_MS, the "held until the read was cut off" pass, and the rule that a negation takes no read at the deadline are gone. A negated poll now takes its last read at the deadline, as a positive one does. An evaluation that returns undefined keeps both polarities waiting.
  2. The three negated callers get the new behavior with no change of their own: locator matchers (packages/e2e/src/expect/async.ts:202), expect(browser) (packages/web/src/browser.ts:1039), and .not.toHaveScreenshot (packages/e2e/src/expect/screenshot.ts:204). The other five callers poll with negated: false.
  3. One thing did rely on the hold. LocatorEngine.tryRead returned a NODE_STALE read as zero matches, and the visibility and attachment matchers evaluate zero matches as "absent". Under the hold the next read corrected it; on a first-read pass it would be a false pass. readOne now reports it as one match with no node and stale: true (packages/core/src/locator/engine.ts:512), the matcher sample carries the flag so conditionEvaluable skips it (packages/e2e/src/expect/async.ts:266), and waitFor samples again (packages/core/src/locator/screen.ts:718). This also closes the same false pass in toBeHidden() and waitFor({ state: 'hidden' }), which never had the hold. A failure whose last read was stale says observed: a node that went stale before it was read instead of hidden or no node.
  4. #2264 clamped the hold to the timeout after a late wake. With the hold gone, its test and its unreleased changeset (negated-poll-deadline-clamp.md) are removed.
  5. Docs: the Negation section and the not doc comment in docs/reference/expect.mdx, pollCondition in docs/reference/engine.mdx, the migration row moved from Planned to the Assertions mapping in docs/migrate/playwright.mdx, and the skill text in skills/e2e and its plugins/e2e copy. The other migrate pages do not mention the hold. Translations are left to lag.

Decisions a reviewer would ask about:

  • .not.toHaveScreenshot loses the hold too. With it, a frame in the middle of a transition did not pass unless the difference lasted 1 s. Playwright passes not.toHaveScreenshot on the first capture that differs, and the positive form already matches on the first capture, so one rule for every matcher won over keeping the window for this one. Say so if you want it kept for screenshots.
  • Two web benchmark tests used the hold as a dwell. order-history used .not.toBeHidden() to tell a stuck loader from a slow one; it now expects toBeHidden({ timeout: 1000 }) on the loader to fail, then checks that no order is listed, so it fails when the loader goes away within that second (checked by swapping the loader for an empty state after 400 ms). newsletter-signup read "no feedback rendered" right after the tap; those reads moved after the console-error poll that proves the handler ran.
  • There is no primitive for "X stays for N ms" any more. The changeset tells users to assert the state that follows. A dwell matcher would be a separate proposal.
  • The stability policy asks for a deprecation window before a breaking change. A timing change has no old form to keep alive beside the new one, so this ships as minor with **Breaking:**, like the other breaking changes queued for 0.25.0.

Issue

ENG-1285

Verified

Ran it locally: yes

  • Testbed, built CLI, real Chromium: node node_modules/e2e/dist/cli/bin.js run tests/browser.e2e.ts. The new test hides a banner and renames the page for 900 ms after a click, then puts both back, so only a read inside that window sees the negation.
main's pollReadsthis PR
× a negated matcher passes on the first read that sees the negation 5.14s ASSERTION_FAILED: expect.not.toBeVisible failed expected: not visible observed: default states (match count 1)✓ a negated matcher passes on the first read that sees the negation 2.40s
  • Each new test fails with its change reverted: the five negated pollCondition unit tests, the scripted-engine test of a negation over a state that lasts 900 ms, .not.toHaveScreenshot passing after one capture (three on main), and the stale-read message test, the four stale-read tests (.not.toBeVisible, .not.toBeAttached, toBeHidden, waitFor), which pass on a node that is still there once the stale flag is dropped again.

  • pnpm check, pnpm test, the testbed suite (619 passed), and the web benchmark suite (95 passed) before the rebase. After the rebase onto 8525d2edfa and the review fixes: pnpm check, pnpm test, the testbed tests/browser.e2e.ts (13 passed), and the two changed web benchmark files.

  • pnpm bench:ab --suite testbed -- tests/controls.e2e.ts tests/browser.e2e.ts: the suite is 60% faster, all of it in expect.not.* steps.

bench-ab output

bench-ab: 679519f1 (merge base with origin/main) vs cc2a8e01

apps/testbed: e2e run --workers 1 tests/controls.e2e.ts tests/browser.e2e.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±5% and 10 ms. darwin arm64, Apple M5 Pro x15, Node 24.19.0, load 2.9.

The suite's tests or config differ between base and head, so each side ran its own. A timing change can be the test change.

Behavior

These tests are left out of the timing rows below.

  • added tests/browser.e2e.ts › browser fixture › a negated matcher passes on the first read that sees the negation (web)

Timings

Every row but the process wall clock covers the 617 tests whose behavior matched.

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)24.22 s (24.18 s-24.42 s)12.50 s (12.48 s-12.53 s)-48.3% (-49.1% to -48.0%)faster
suite (sum of attempts)23.46 s (23.42 s-23.58 s)9.38 s (9.35 s-9.40 s)-60.0% (-60.5% to -59.8%)faster
action (events)3.80 s (3.76 s-3.82 s)3.77 s (3.75 s-3.80 s)-0.2% (-2.1% to +0.6%)same, under the floor
step expect.not.toBeFocused5.02 s (5.02 s-5.03 s)7 ms (6 ms-8 ms)-99.9% (-99.9% to -99.8%)faster
step expect.not.toBeChecked2.01 s (2.01 s-2.01 s)3 ms (2 ms-3 ms)-99.9% (-99.9% to -99.8%)faster
step expect.not.toBeInViewport2.01 s (2.01 s-2.01 s)20 ms (19 ms-32 ms)-99.0% (-99.1% to -98.2%)faster
step expect.not.toBeSelected2.01 s (2.01 s-2.01 s)2 ms (2 ms-3 ms)-99.9% (-99.9% to -99.9%)faster
step app.open1.45 s (1.45 s-1.49 s)1.43 s (1.42 s-1.43 s)-1.8% (-6.7% to -0.4%)unresolved
step expect.toBeEnabled1.24 s (1.24 s-1.24 s)1.25 s (1.24 s-1.25 s)+0.4% (-0.1% to +0.7%)same, under the floor
step expect.not.toHaveClass1.01 s (1.00 s-1.01 s)2 ms (1 ms-2 ms)-99.8% (-99.9% to -99.8%)faster
step expect.not.toBeExpanded1.01 s (1.00 s-1.01 s)17 ms (17 ms-17 ms)-98.3% (-98.4% to -98.3%)faster
step expect.not.toHaveAttribute1.00 s (1.00 s-1.00 s)1 ms (1 ms-1 ms)-99.9% (-99.9% to -99.9%)faster
step locator.check1.00 s (985 ms-1.02 s)982 ms (972 ms-999 ms)-2.7% (-3.5% to +1.9%)same
step locator.uncheck902 ms (900 ms-906 ms)915 ms (911 ms-922 ms)+1.4% (-1.7% to +2.7%)same
step locator.tap852 ms (838 ms-883 ms)856 ms (846 ms-871 ms)-0.6% (-2.3% to +4.5%)same, under the floor
step locator.longPress753 ms (750 ms-756 ms)751 ms (750 ms-753 ms)-0.3% (-0.9% to +0.5%)same, under the floor
step expect.toBeInViewport523 ms (514 ms-523 ms)524 ms (513 ms-529 ms)+0.2% (-3.0% to +4.5%)same, under the floor
step locator.setInputFiles355 ms (353 ms-368 ms)357 ms (356 ms-369 ms)+0.7% (-2.4% to +3.5%)same, under the floor
step expect.toHaveText335 ms (334 ms-347 ms)334 ms (329 ms-338 ms)-1.8% (-4.8% to +0.3%)same, under the floor
step locator.press97 ms (95 ms-100 ms)93 ms (90 ms-95 ms)-5.6% (-12.8% to +1.9%)same, under the floor
step locator.focus83 ms (82 ms-84 ms)82 ms (82 ms-84 ms)-0.6% (-9.9% to +1.8%)same, under the floor
step locator.fill63 ms (61 ms-65 ms)64 ms (63 ms-65 ms)+0.1% (-7.1% to +6.5%)same, under the floor
step expect.toHaveCount57 ms (56 ms-59 ms)53 ms (52 ms-54 ms)-7.0% (-10.1% to -5.4%)same, under the floor
step locator.scrollIntoView53 ms (45 ms-62 ms)52 ms (36 ms-66 ms)+14.5% (-44.7% to +57.4%)same, under the floor
step app.restart45 ms (44 ms-47 ms)45 ms (44 ms-45 ms)0.0% (-7.3% to +2.3%)same
step locator.blur32 ms (30 ms-33 ms)31 ms (31 ms-32 ms)-1.5% (-15.3% to +5.1%)same, under the floor
step locator.selectOption29 ms (29 ms-34 ms)24 ms (23 ms-24 ms)-19.0% (-40.8% to -14.3%)same, under the floor
step locator.secondaryTap29 ms (27 ms-52 ms)30 ms (29 ms-41 ms)+3.5% (-29.9% to +36.1%)same, under the floor
step locator.doubleTap27 ms (25 ms-29 ms)28 ms (27 ms-30 ms)+5.7% (-5.1% to +19.1%)same, under the floor
step expect.toBeFocused27 ms (27 ms-28 ms)27 ms (26 ms-29 ms)-1.9% (-10.4% to +7.9%)same, under the floor
step browser.setNetwork25 ms (25 ms-25 ms)26 ms (25 ms-27 ms)+1.9% (0.0% to +14.0%)same, under the floor
step expect.toBeVisible22 ms (21 ms-24 ms)22 ms (20 ms-22 ms)-6.6% (-10.8% to +2.4%)same, under the floor
step expect.toBeHidden21 ms (20 ms-21 ms)18 ms (18 ms-18 ms)-12.2% (-20.4% to -10.0%)same, under the floor
step expect.toHaveAttribute19 ms (19 ms-19 ms)19 ms (18 ms-19 ms)0.0% (-7.4% to +2.9%)same
step expect.toContainText19 ms (18 ms-19 ms)19 ms (18 ms-19 ms)0.0% (-2.5% to +2.7%)same
step expect.toBeSelected18 ms (17 ms-20 ms)17 ms (17 ms-18 ms)-5.6% (-12.5% to +6.1%)same, under the floor
step expect.toBeDisabled17 ms (16 ms-18 ms)17 ms (16 ms-17 ms)-5.9% (-15.2% to +9.5%)same, under the floor
step browser.reload15 ms (14 ms-17 ms)15 ms (14 ms-15 ms)-3.0% (-17.9% to +7.4%)same, under the floor
step browser.evaluate8 ms (8 ms-9 ms)8 ms (7 ms-8 ms)-10.6% (-12.5% to 0.0%)same, under the floor
step expect.toHaveValue8 ms (7 ms-8 ms)7 ms (7 ms-8 ms)0.0% (-26.1% to +8.0%)same
step expect.toHaveClass7 ms (6 ms-8 ms)4 ms (2 ms-4 ms)-50.0% (-69.1% to -32.4%)same, under the floor
step browser.goto4 ms (4 ms-4 ms)4 ms (4 ms-4 ms)0.0% (-10.6% to +29.1%)same
step browser.back4 ms (3 ms-4 ms)4 ms (3 ms-4 ms)0.0% (-22.5% to +15.5%)same
step expect.toBeChecked4 ms (3 ms-4 ms)4 ms (3 ms-4 ms)0.0% (-22.5% to 0.0%)same
step expect.toHaveTitle3 ms (2 ms-3 ms)2 ms (2 ms-2 ms)-33.3% (-42.3% to +22.5%)same, under the floor
step browser.forward3 ms (3 ms-3 ms)3 ms (3 ms-3 ms)0.0% (0.0% to 0.0%)same
step expect.toHaveAccessibleName2 ms (1 ms-3 ms)1 ms (1 ms-1 ms)-50.0% (-75.0% to 0.0%)same, under the floor
step browser.setViewport2 ms (1 ms-2 ms)1 ms (1 ms-2 ms)0.0% (-50.0% to +100.0%)same
step browser.addInitScript1 ms (0 ms-1 ms)1 ms (0 ms-1 ms)0.0% (0.0% to 0.0%)same
step browser.setCookies1 ms (0 ms-1 ms)1 ms (0 ms-1 ms)0.0% (0.0% to 0.0%)same
step browser.cookies1 ms (0 ms-1 ms)1 ms (0 ms-1 ms)0.0% (0.0% to 0.0%)same
step browser.emulateMedia1 ms (1 ms-1 ms)1 ms (1 ms-2 ms)0.0% (0.0% to +100.0%)same
step expect.toBeAttached1 ms (1 ms-1 ms)1 ms (1 ms-1 ms)0.0% (0.0% to 0.0%)same
step expect.toBeExpanded1 ms (1 ms-1 ms)2 ms (1 ms-2 ms)0.0% (0.0% to +100.0%)same
step expect.toHaveURL1 ms (0 ms-1 ms)1 ms (0 ms-1 ms)+100.0% (+100.0% to +100.0%)same, under the floor
step browser.title0 ms (0 ms-0 ms)1 ms (0 ms-1 ms)0.0% (0.0% to 0.0%)same

Counters

Counterbasehead
attempts2930
events engine3840
events poll108110
steps210218
tests passed2930
Tests outside the threshold: 9 of 617
Testbase median (IQR)head median (IQR)change (95% CI)verdict
tests/controls.e2e.ts › controls › a link reads back its attributes, accessible name, classes, and text (web)2.10 s (2.09 s-2.10 s)80 ms (78 ms-82 ms)-96.2% (-96.3% to -96.0%)faster
tests/controls.e2e.ts › controls › blur removes focus from MathML matched by label (web)1.10 s (1.09 s-1.10 s)84 ms (82 ms-84 ms)-92.4% (-92.6% to -92.1%)faster
tests/controls.e2e.ts › controls › blur removes focus only from the matched node (web)1.11 s (1.11 s-1.12 s)104 ms (102 ms-106 ms)-90.7% (-90.9% to -90.5%)faster
tests/controls.e2e.ts › controls › blur works on an input matched by its current value (web)1.13 s (1.12 s-1.13 s)112 ms (110 ms-115 ms)-90.0% (-90.2% to -89.5%)faster
tests/controls.e2e.ts › controls › check and uncheck move checked state, and one radio checks at a time (web)2.22 s (2.21 s-2.23 s)223 ms (221 ms-239 ms)-90.0% (-90.1% to -89.1%)faster
tests/controls.e2e.ts › controls › expanded state follows the disclosure toggle (web)1.11 s (1.10 s-1.12 s)126 ms (116 ms-134 ms)-88.8% (-89.6% to -87.8%)faster
tests/controls.e2e.ts › controls › focus moves with Tab and Shift+Tab (web)2.14 s (2.13 s-2.15 s)115 ms (113 ms-117 ms)-94.6% (-94.8% to -94.4%)faster
tests/controls.e2e.ts › controls › selecting an option moves selected state onto it (web)2.12 s (2.11 s-2.13 s)103 ms (102 ms-104 ms)-95.2% (-95.2% to -95.1%)faster
tests/controls.e2e.ts › controls › toBeInViewport follows the scroll position, and no match is outside the viewport (web)2.67 s (2.65 s-2.68 s)696 ms (657 ms-705 ms)-73.7% (-76.0% to -72.9%)faster

With 617 tests compared at 95%, about 30.9 could land outside by chance.

  • Docs screenshots: none. The changed text is three table rows and one short section, and mint dev does not build this site locally.

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). Title and changeset: yes. No deprecation window, see "How it works".
  • 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. No type changed: only the doc comment on AsyncExpectation.not.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile. pollCondition keeps its signature; the changeset covers @e2e-dev/core and e2e.