fix(expect): read a missing frame as zero matches in toHaveCount

Merged
SA
21d ago
+101-55

What and why

  • After an iframe closes, expect(browser.frameLocator('#checkout').getByRole('button')).toHaveCount(0) polled until the timeout and failed LOCATOR_NOT_FOUND: no frame matches #checkout. not.toHaveCount(2) and toHaveText([]) failed the same way. On the same locator, count() returned 0 and toBeHidden() passed.
  • Cause: the whole-set poll samples (sample() in packages/e2e/src/expect/async.ts) went through resolveAll and readAll, the only reads that still passed missingFrame: 'wait'. count(), all(), allTextContents(), and the visibility matchers already read a missing frame as zero matches.
  • Those samples now call resolveNow and readAllNow with the poll's deadline. resolveAll and readAll duplicated them after the change, so they are gone.
  • A positive expectation still waits for the frame. toHaveCount(1) keeps polling and then fails ASSERTION_FAILED with observed: count 0, not LOCATOR_NOT_FOUND. The list form of toHaveText fails with observed: text [].
  • This matters for Cypress users: the migration guide maps .should('not.exist') to .toHaveCount(0).
  • docs/reference/expect.mdx (Strictness) now lists these matchers among those that read a missing frame as zero matches.

Issue

Fixes #1353

Verified

Ran it locally: yes

  • Unit: tests/unit/expect-missing-frame.test.ts fails on main with LOCATOR_NOT_FOUND (3 of 4 tests) and passes here.
  • Testbed, real Chromium through the built CLI: node node_modules/e2e/dist/cli/bin.js run tests/zz-frame-close.e2e.ts --workers 1. The test is an uncommitted scratch file. It serves a cart page with browser.route whose button removes <iframe id="checkout">, taps the button, then runs each assertion on browser.frameLocator('#checkout').getByRole('button'). The toHaveCount(1) case is expected to fail, and it shows what the failure says.
mainthis branch
count() is 0: passedpassed
toBeHidden(): passedpassed
toHaveCount(0): failed, LOCATOR_NOT_FOUND after 6.4 spassed
not.toHaveCount(2): failed, LOCATOR_NOT_FOUNDpassed
toHaveText([]): failed, LOCATOR_NOT_FOUNDpassed
toHaveCount(1): failed, LOCATOR_NOT_FOUNDfailed, ASSERTION_FAILED, observed: count 0

main:

   ✓ closed checkout frame > count() is 0 1.44s
   ✓ closed checkout frame > toBeHidden passes 801ms
   × closed checkout frame > toHaveCount(0) passes 6.38s
   × closed checkout frame > not.toHaveCount(2) passes 6.01s
   × closed checkout frame > toHaveText([]) passes 5.97s
   × closed checkout frame > toHaveCount(1) fails on the count 2.02s
LOCATOR_NOT_FOUND: no frame matches #checkout: frameLocator("#checkout") >> getByRole("button")

this branch:

   ✓ closed checkout frame > count() is 0 503ms
   ✓ closed checkout frame > toBeHidden passes 354ms
   ✓ closed checkout frame > toHaveCount(0) passes 313ms
   ✓ closed checkout frame > not.toHaveCount(2) passes 1.32s
   ✓ closed checkout frame > toHaveText([]) passes 363ms
   × closed checkout frame > toHaveCount(1) fails on the count 1.35s
ASSERTION_FAILED: expect.toHaveCount failed
locator: frameLocator("#checkout") >> getByRole("button")
expected: count 1
observed: count 0 (match count 0)

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.

🤖 Generated with Claude Code