Repository navigation
feat(test)!: set fixtures up on demand, once per worker, and in beforeAll - #1891
Conversation
test.extend() takes [fn, { scope: 'worker' }]: one instance per worker and
target, torn down when the worker ends. Suite hooks receive worker fixtures.
Engines can declare isolation: 'worker'; @e2e-dev/mobile does, so its device,
app, and screen reach beforeAll, afterAll, and worker fixtures.
Refs ENG-1297
🟢 e2e web: 93 passedAll 93 tests in 42 files
Tested by e2e 0.23.1 · 1m 2s · web · run artifacts |
🟢 e2e web agent: 34 passed35 agent steps · 26 replayed from cache · 25 model calls · 119.7k tokens (94% cached) · $0.0049 All 34 tests in 5 files
Tested by e2e 0.23.1 · 1m 38s · web · run artifacts |
🟢 e2e android agent: 20 passed24 agent steps · 15 replayed from cache · 36 model calls · 200.2k tokens (85% cached) · $0.0124 All 20 tests in 1 file
Tested by e2e 0.23.1 · 5m 38s · android-emulator · run artifacts |
🟢 e2e ios: 56 passed, 1 skippedAll 57 tests in 7 files
Tested by e2e 0.23.1 · 7m 7s · ios-simulator · run artifacts |
🟢 e2e ios agent: 21 passed, 1 skipped25 agent steps · 12 replayed from cache · 59 model calls · 375.2k tokens (82% cached) · $0.0252 All 22 tests in 1 file
Tested by e2e 0.23.1 · 8m 44s · ios-simulator · run artifacts |
🟢 e2e android: 58 passed, 1 skippedAll 59 tests in 7 files
Tested by e2e 0.23.1 · 8m 47s · android-emulator · run artifacts |
|
[claude-opus-5-5] responding on behalf of KrzysztofMoch For the human review, two calls this PR leaves to you (both also in the body):
Android showed one first-test flake per job (home list not visible within 5 s after launch, passed on retry); the same test flakes on unrelated PRs. |
A test now sets up only the test.extend() fixtures its body and its
beforeEach and afterEach hooks name in their first parameter, the fixtures
those name, and the auto ones. A suite hook sets up the worker fixtures it
names and the auto worker fixtures. Names are read from the function's
source, as Playwright and Vitest read them.
- [fn, { auto: true }] and [fn, { scope: 'worker', auto: true }] run a
fixture nothing names.
- A fixture can name the fixtures declared beside it in the same extend()
call. Fixtures run in declaration order, each after the ones it names.
- The attempt is resolved as one unit before the session launches: a
fixture only an afterEach names is set up before the first beforeEach.
An afterEach that names a fixture whose setup failed is skipped.
- Fixture rules fail as COLLECTION_ERROR with the file and line of the
code: a parameter that is not destructured or takes a rest property, and
a cycle, when the file is collected; a name the target lacks, a test
fixture named by a suite hook or a worker fixture, and a fixture named
like an engine's, at selection, for the pairs that run on that target.
- On an engine with isolation: 'worker', a suite hook or a worker fixture
teardown that names no engine-backed fixture opens no engine attempt.
BREAKING CHANGE: a fixture nothing names no longer runs unless it is
declared with auto: true, and a test body, hook, or fixture function that
takes the whole fixture object (async (fixtures) => {}) no longer loads.
Refs ENG-1297
The five core fixtures (agent, app, screen, platform, session) are one table in @e2e-dev/core: name, scope, and factory. createFixtures builds the built-ins object through one loop over that table and the engine's factories, every fixture a lazy accessor behind the same gate as before. The runner's fixture pool, test.extend() validation, and launch read the names and scopes from the table, so the name lists and the per-name scope checks are gone. A launch session exposes the core fixtures that do not belong to a test: app, screen, platform. No behaviour change. app and screen were built with the attempt and are now built on first read, like agent and the engine's fixtures. Refs ENG-1297
A test.extend() fixture named like one the test already has extends it,
by Playwright's rule: inside its function its own name is the fixture it
extends (the built-in, or an earlier fixture of that name), and tests,
hooks, and other fixtures that name it receive what it hands to use().
base.extend({ device: async ({ device }, use) => {
await device.setPermission('camera', 'deny');
await use(device);
} })
- A plain function takes the scope the extended fixture has on the
target: device is a worker fixture on mobile, so its override runs once
per worker and reaches beforeAll; app is a test fixture on web and a
worker fixture on mobile. A declared scope that differs is a
COLLECTION_ERROR naming both.
- A worker-scoped override receives what a worker fixture receives for
that built-in, the forwarder to the attempt that is running.
- On a target without the built-in nothing fails until code that runs
there names it; then the target has no such fixture.
- Only user code sees the override: the attempt's own built-ins, which the
runner and the worker fixtures' forwarders read, are untouched.
- Types: the override keeps the extended fixture's type and scope, through
an extend() overload that takes overrides without type arguments.
A fixture named like a core fixture, an engine's, or an earlier one of the
chain was refused before.
Refs ENG-1297
The docs now use one term, built-in fixtures, for agent, app, screen, platform, session, and the ones an engine contributes (browser, device), where pages said core fixtures, contributed fixtures, or engine fixtures. The test reference gets one table with each built-in fixture's scope on web and on mobile, and says what the scope decides: a test fixture reaches test bodies, beforeEach, and afterEach; a worker fixture also reaches beforeAll, afterAll, and worker fixtures. The hooks, worker fixtures, and override sections link to it, as do the fixtures guide and the engine pages. The Japanese and Chinese pages carry the table and the same terms. Refs ENG-1297
A fixture that extends `app` or `screen` was taken for a test fixture when its test.extend() call was checked, before any target is known. A worker fixture declared after it that names `app` then failed at import on every target, though the same fixture is accepted without the override and the plan resolves both to worker scope on an engine whose state outlives a test. The scope of a plain function is now unresolved until a target is known, whatever it extends, as it already was for an engine's own fixtures. Refs ENG-1297
On an engine whose state outlives a test, a beforeAll or afterAll hook, or a worker fixture's teardown, can start a device step and not await it. The hook's attempt then closed under the step, and its failure arrived later as a stray rejection charged to whichever test ran then. The suite attempt now does what a test attempt does once its body settled: the step is failed at its call and cancelled, its outcome is awaited within the cleanup budget, and the hook fails with STEP_NOT_AWAITED (HOOK_FAILED for a suite hook). The suite attempt keeps no step records, and StepRecorder dropped the record of the step it had just opened before marking it running, so there was nothing to abandon: a step now counts as running before the retained window is trimmed. Refs ENG-1297
Registration parsed each test body, hook, and fixture function for the fixtures it names, dropped the result, and left it in a WeakMap that the planner and the pool read back with placeholder subjects. A cache miss there would have named the wrong code and no line. The parse result is now data: `names` on the registered test, the hook, and the fixture definition. The planner and the pool read it, the WeakMap and the per-function wrappers are gone, and one describeUser() gives an error its subject. The explore test, registered by hand, parses its body where it builds the record. Unit tests state the names instead of generating source text for them. Refs ENG-1297
The core table declared the core fixtures, but what a target's built-in fixtures are was still assembled around it: core names plus engine names in three files, the 'engine' scope resolved in e2e through a cast into the table, `platform` hard-coded as the one fixture that needs no attempt, and two structural copies of the target. core now exports builtInFixtures(target): names, scope(name), needsAttempt(name), attemptless(name), and the unknown-fixture message, over one BuiltInTarget. `platform` declares in the table that the target alone decides it. launch, the planner, and the pool read from it. DECLARED_BUILT_INS is the same view before a target is known: a scope the engine decides is 'unresolved'. The planner takes either one, so its no-target mode and the `target === undefined` branches through scopeOf and read are gone. The scope of a fixture that extends `app` before a target is known is 'unresolved' by construction. Refs ENG-1297
Every test body, hook, and fixture function must destructure its first parameter, so what it reads is known before it runs. bindFixtures still built a Proxy-gated bag for arbitrary property reads: one value in three stores, the gate run twice (once over a faked plan), a third derivation of scope, and an own-name shadow that re-derived what the planner had resolved. No test code could reach the gate's throwing branches; only unit tests did, by reading the bag directly. The plan now records, per function, what each name it reads resolved to: a chain fixture or a built-in. The pool hands each function a plain object with exactly those names, the value a chain fixture handed over, or a built-in read off the attempt when the function destructures it, so built-ins stay lazy. An override that names itself receives the fixture it extends because that is what its own name resolved to. - bound.call(user) replaces user.fn(bound.fixtures); ready(user) asks whether every chain fixture it names was provided. - attemptlessBuiltIns is a function from a name to a value that throws the reason for a built-in that needs an attempt. - The planner runs once: it records what each fixture names, and the order is one declaration-order walk over that record. A second validating pass only reordered. Behaviour: a bound or native function, whose parameters cannot be read, is a COLLECTION_ERROR; it used to parse as naming nothing and still got lazy built-ins. The docs no longer promise hooks and the body one shared object. Refs ENG-1297
dispose, openSuiteHooks, and openSuiteAttempt were 177 lines of a second
attempt lifecycle inside TargetExecutor, written as closures in one method.
run/suite-attempt.ts now owns it: a SuiteAttempt class with open, acquire,
within, and close, over a SuiteAttemptHost interface the executor satisfies
(the way serial.ts borrows it through SerialHost), plus the worker
fixtures' teardown pass. It is the realm's SuiteHookSession itself, so the
executor keeps a context object and two call sites.
The three branches that built an attemptless reader with a "not available
to the <label>" error are one NoAttempt value ('isolated', 'unnamed',
'interrupted', or the launch failure) and one mapper to the message.
execute.ts: 1244 lines to 1052.
Refs ENG-1297
A run's attempt and an `e2e mcp` session's test each planned the test's fixtures, bound them, entered the worker pool, set them up, ran the beforeEach hooks and the body, filtered the afterEach hooks that can still run, and labelled the teardowns. This PR widened that copy, and standalone-test.ts asked the realm for the same hooks four times. planAttemptFixtures(target, test, hooks) plans before any session exists and returns open(), which binds the plan to the attempt: enter(), run() for setup, hooks, and body, and cleanup() for the labelled afterEach hooks and teardowns. Both call sites keep their own phases, budgets, and error handling around it. execute.ts: 1052 lines to 1031, its size on main. Refs ENG-1297
withSuiteHooks handed its work a thunk that opened the pass on first use. Opening one does no I/O: the suite attempt launches nothing until a hook that names an engine fixture runs. The pass is now opened up front and closed in finally, and the `opened` slot and the `hooks()` call form go. Refs ENG-1297
The selection-time fixture check rebuilt the scope chain of a test and filtered the file's hooks by it, which the realm already did its own way. registry.ts now exports scopeChain(test) and hooksFor(registration, test), and both use them. Refs ENG-1297
types.ts grew from 1679 to 1798 lines in this PR. The fixture scope, the worker fixtures, and what test.extend() takes (definitions and overrides) are one coherent group that nothing else in the file defines against, so they move to types/fixtures.ts and types.ts re-exports them. The public surface is the same. types.ts: 1798 lines to 1715. Refs ENG-1297
A fixture that extends one the base already has was typed three ways: a conditional branch inside the `Extra` map of FixtureDefinitions, the `& FixtureOverrides` intersection, and the extend(overrides) overload. Compiling the type tests without each in turn: the intersection types an override written beside new fixtures with type arguments, and the overload types an override-only call without them; both fail the tests when removed. The conditional branch does not: with it gone the tests pass, and a base name in `Extra` is `never` again, as it was before overrides. It is removed, and the reference's copy of the declaration follows. Refs ENG-1297
fixture-names.ts parses a parameter list, and also held FixtureUseError and declarationSite, which format an error. They move to fixture-errors.ts. Its identifier pattern held two raw zero-width characters; they are written as escapes now. hookLog and LOG_MODULE were pasted into three integration tests; they are one helper in tests/helpers/hook-log.ts. Refs ENG-1297
The summary of assertFixtureUse still listed a chain fixture named like an engine's as an error. Such a fixture extends the built-in now; the rule left is a declared scope that differs from the extended fixture's. Refs ENG-1297
Two more pieces that are not the attempt's own sequence leave the file. - Holding steps for `e2e run --step`: reading the screen a hold starts on and wiring AttemptHolds to the runner's hook move to step-hold.ts, beside the holds they configure (holdAttemptSteps, readHeldScreen). - The result of a finished test or setup unit is a pure function of the pair, the verdict, the attempts, and a beforeAll failure: unitResult in units.ts, beside pairResult. execute.ts: 1031 lines to 956. It was 1031 on main and 1226 at the review. Refs ENG-1297
Main's #1880 names an unlabeled step by its api alone ("gadget.hang finished"), where the message used to quote an empty label. The suite-hook test's pattern required a label between the two, so it failed on the merge with main.
A fixture that extends another and does not state `auto` now has the extended fixture's, the way it has its scope: an override of an auto fixture is set up for every test, and one of a built-in fixture on demand. An `auto` that differs from the extended fixture's is a COLLECTION_ERROR naming both, worded like the scope mismatch. This matches Playwright. It also fixes which fixtures the plan starts from. An auto fixture was a root by its own flag, so with an auto base and its override in one chain the base was set up and the override skipped, while the name resolved to the override; an override that replaced the base without naming it still left the base running. An auto fixture is now a root only where its name stands for it: the base runs once through the override's own name, or not at all when the override replaces it. Refs ENG-1297
Main's #1880 rewrote the STEP_NOT_AWAITED message line that the next commit changes, so the branch needs it to stay mergeable.
STEP_NOT_AWAITED said "the test body returned before ..." for a step a beforeAll, afterAll or beforeEach hook, a fixture's setup or a worker fixture's teardown left running. The recorder now keeps the owner each step was started under, and the runner says which code runs: the pool for fixtures, hooks and the body, the suite attempt for suite hooks, and a hosted session for itself.
test.extend()fixtures ran for every test registered through thattest, named or not, so each setup needed its own exportedtest.beforeAllandafterAllsawplatformonly, and nothing could be set up once per worker: the mobile benchmark kept its one-install-per-device promise onglobalThis. Closes ENG-1297.Breaking: fixtures are set up on demand
beforeEachandafterEachhooks, and those fixtures name by destructuring their first parameter. A fixture nothing names no longer runs.async (fixtures) => {},async (_, use) => {}, a rest property, and a bound function are aCOLLECTION_ERRORwith the file and line; a fixture that needs nothing writesasync ({}, use) => {}.auto: true.extend(), in any order; a cycle is aCOLLECTION_ERROR.Worker fixtures, and
beforeAllgets them[fn, { scope: 'worker' }], typed with the second type argument ofextend. Set up once per worker and target, the first time a test or suite hook names it, torn down when the worker ends, last set up first.beforeAllandafterAllreceive worker fixtures:platform, the worker fixtures of their chain, and on a'worker'engine the engine's fixtures,app, andscreen.SuiteFixturesandSuiteHookFnstay as deprecated aliases.isolation: 'worker'(defineEngine, the protocol manifest,engine-manifest-1schema).@e2e-dev/mobiledoes: a device keeps its state between attempts.buildfixture is{ scope: 'worker', auto: true }; theglobalThismap is gone.Extending a built-in fixture
extend({ app: async ({ app }, use) => { await app.open('/dashboard'); await use(app); } })wraps a built-in, or an earlier fixture of the chain. Inside, the name is the fixture it extends; everyone else gets what it passes touse.autoof what it extends; ascopeorautothat differs is aCOLLECTION_ERRORnaming both. So adeviceoverride on mobile runs once per worker and reachesbeforeAll.extendwas refused before.How it runs
flowchart LR C[collect: fixtureNames per body, hook, fixture] --> S[select: planAttempt / planSuiteHook per target] S --> W[worker: bindFixtures from the plan] W --> T{scope} T -- test --> A[set up in the attempt, torn down after afterEach] T -- worker --> P[WorkerFixturePool: one instance per definition, torn down in dispose] H[beforeAll / afterAll] --> E{engine isolation} E -- attempt --> PL[platform + worker fixtures] E -- worker --> Q{serial group or e2e mcp?} Q -- yes --> O[the open attempt] Q -- no --> N[short attempt of its own, results/suite-hooks] N -- cannot open / interrupted --> F[hooks still run; device, app, screen throw the reason]packages/e2e/src/collect/fixture-names.tsreads names;fixture-plan.tsresolves order, scope, auto, cycles, and whether code reaches the engine;fixture-use.tsruns the plan at selection for pairs that run.packages/e2e/src/run/fixture-pool.tsreplacesextended-fixtures.ts. A worker fixture holds forwarders fordevice,app, andscreen, so its steps land in whichever test or hook is running.packages/e2e/src/run/suite-attempt.tsopens the short attempt for suite hooks and worker teardowns on a'worker'engine. A failed open is one run error in phaselaunch.CORE_FIXTURESinpackages/core/src/run/fixtures.ts, each with its scope;appandscreenfollowengine.isolation.Decisions left to the reviewer
@e2e-dev/mobilekeepse2eand@e2e-dev/coreat>=0.23.2, right only if this ships in 0.23.2. If 0.23.2 is cut first, raise both.use(), which resumes in the setup's context. Left as a follow-up.Made with Claude Opus 5.5 (claude-opus-5-5) in Claude Code, running in T3 Code.