fix(runner): distinguish worker fixtures by import URL (ENG-1553)

MI
7h ago
+1159-88

What

On Node.js, worker fixtures distinguish user-written ES module queries and
fragments. When the runner reloads the same import for a retry or hook
registration, the worker reuses that fixture value. Reuse holds when another
resolver appends a fragment or query parameter, rewrites or percent-encodes the
query, appends a literal ? query onto the URL's query, or short-circuits a
test entry started by importFreshModule. The inputs where reuse does not hold,
such as an import with only a fragment under a resolver that appends text after
it, are under "Tradeoffs and limits". Fresh config graphs (0.18.0 or later)
and registrar collections (0.25.0 or later) started by an older copy
of e2e in the same process stay fresh when that entry reaches this loader's
resolve hook with the accepted parameter shape.

Why

Worker fixture identity previously keyed on file path alone, which merged
imports such as ./db.ts?a and ./db.ts?b into one instance. Keying on raw
import.meta.url instead would break retries, because each fresh runner load
appends a new ?e2e=<key> query parameter and a second setup of an exclusive
port or file lock fails. Reading any e2e or e2e-graph query parameter from
the URL as loader metadata also mistook user-written parameters under those
names for the runner's own.

How it works

flowchart LR
  A[importFreshModule] --> S[keys + graphs sets]
  B[legacyEntry in resolve] --> S
  B --> C[legacyCollectorLoads WeakMap]
  U[module URL] --> W[writtenIn]
  S --> W
  W -->|key + graph| R[resolve + load hooks]
  C -->|active older-copy collection| R
  W -->|original URL| F[fixtureIdentities]
  1. Shared parameter sets. FreshnessState on freshnessSlot
    (packages/e2e/src/config/esm-hooks.ts:87, :111) shares atWork, the
    load sequence, and decoded keys and graphs sets across copies of e2e
    in one process. importFresh (packages/e2e/src/config/load.ts:137)
    writes new keys as <cacheKey>-<n>-fresh, and importFreshModule
    (packages/e2e/src/config/esm-hooks.ts:205) records key and graph
    before importing the entry. An older copy keys its loads <cacheKey>-<n>,
    and its cache keys can start with a target's name, so it can write any key
    that ends in a number; the -fresh suffix keeps this copy's keys out of
    that set, where a fresh- prefix did not.
  2. One URL reader. loaderParameter and writtenIn
    (packages/e2e/src/config/esm-hooks.ts:435, :453) inspect the query
    before #, preserve any leading ? inside a parameter name, match decoded
    e2e and e2e-graph values against keys and graphs (including
    percent-encoded names or values and a naive resolver appending ?x=1
    directly onto a loader parameter), and rebuild original from the remaining
    raw parameters, their literal ? or & separators, and the fragment.
    resolve (packages/e2e/src/config/esm-hooks.ts:484), load
    (packages/e2e/src/config/esm-hooks.ts:502), and originalModuleURLSlot
    (packages/e2e/src/config/esm-hooks.ts:569) all read writtenIn.
    appendQuery (packages/e2e/src/config/esm-hooks.ts:177) appends loader
    parameters without re-encoding user query text or fragments.
  3. Older copies in the same process. Published versions 0.18.0 through
    0.26.0 write ?e2e=<key> and optional &e2e-graph=<name> on the entry
    imported from dist/config/load.js, and hold any active collector on
    e2e.activeCollector.v1. isConfigLoader and legacyEntry
    (packages/e2e/src/config/esm-hooks.ts:367, :403) resolve the importer
    path with fileURLToPath (ignoring any query or fragment added to
    config/load) and read the written specifier query even if an earlier
    resolver modifies resolution.url. While collection is active,
    legacyEntry binds the key to the collector in legacyCollectorLoads
    (WeakMap, packages/e2e/src/config/esm-hooks.ts:121, :123, :171). On
    0.25.0 and 0.26.0, which report registrar modules via e2e.registrar-module.v1,
    transitive plain helpers re-evaluate registrars during active collection and
    stop inheriting that load once collection ends.
  4. Fixture identity and older-first fallback. fixtureIdentities
    (packages/e2e/src/collect/fixture-identity.ts:25) keys worker fixtures by
    originalModuleURL(location) ?? location when the slot is present, and
    falls back to moduleFile(location)
    (packages/e2e/src/collect/fixture-identity.ts:32) when an older copy
    registered the module loader first and installed no lookup.
  5. Documentation and release note. docs/fixtures.mdx,
    docs/reference/test.mdx, skills/e2e/references/writing-tests.md,
    plugins/e2e/skills/e2e/references/writing-tests.md, and
    .changeset/silent-fixture-urls.md describe synchronous registration on
    Node.js, ES module URL query and fragment identity, the file-path fallback
    when an older version owns the loader or test.extend from an older
    version, 0.25.0 or later, declares the fixture, another resolve hook
    that changes a module's URL between loads, when a test file reloads (after a failed test and for each
    serial group and setup test), and Bun test-file reload behavior. The
    fixture guide links to the test reference for the mixed-version cases and
    for the resolve hook case. All four pages also say that a reload keeps the
    fixture's value, not the module's variables: a fixture that returns an
    object from module scope hands later test files the first file's object.

Tradeoffs and limits

  • If user code writes an import query whose decoded e2e or e2e-graph
    parameter equals a live key in keys or graph in graphs (or starts
    with one followed by a literal ?, such as ?e2e-graph=<live>?x=1),
    writtenIn reads that prefix as the loader's, matching how main treats
    every e2e and e2e-graph parameter. The same user-written URL therefore
    reads differently in one process once that load key or graph name becomes
    live.
  • inGraph (packages/e2e/src/config/esm-hooks.ts:315) skips the fresh-graph
    tag when a resolved URL already has any e2e-graph parameter, and
    inFreshLoad (packages/e2e/src/config/esm-hooks.ts:162) skips the load key
    when it already has any e2e parameter. A config dependency imported with a
    user-written ?e2e-graph=app stays cached across fresh graphs, as on main.
  • A resolver that mutates a URL's fragment text differently after the loader
    adds ?e2e=<key> (for example appending ?x=1 versus &x=1 after #frag),
    or that concatenates an unescaped ?x=1 onto the query and then re-encodes
    it into %3Fx%3D1 via searchParams.sort(), produces distinct module
    identities across reloads.
  • keys and graphs retain one short string per fresh load key and graph name
    created in the process, and legacyConfigParents caches one boolean per
    distinct config/load file path; legacyCollectorLoads is a WeakMap and
    retains nothing after a collection finishes. Failed or symlinked imports add
    no per-module entry.
  • Recognizing an older copy's entry requires its import from dist/config/load.js
    or src/config/load.ts in a package named e2e to reach this loader's
    resolve hook with the accepted parameter shape (?e2e=<key> and optional
    &e2e-graph=<name>, nothing else). A bundled or relocated older copy, or a
    resolver registered after this loader that short-circuits the entry or adds
    extra query parameters to specifier before resolve runs, is not matched.
  • When an older copy registered the module loader first, its resolve hook
    owns the process: worker fixtures use file-path identity, and two-install
    runner.list() calls reaching a shared registrar through plain helpers keep
    0.25.0 and 0.26.0's own two-install behavior (a, then b, then a and
    b) because those releases store atWork in module-private AsyncLocalStorage. Releases
    0.18.0 through 0.24.0 predate e2e.registrar-module.v1 and do not mark
    imported helpers as registrars.
  • A fixture declared with test.extend from 0.25.0 or 0.26.0 keeps
    file-path identity when this loader registered first too: the copy that runs
    test.extend names the fixture (packages/e2e/src/collect/registry.ts:776),
    and those releases name it by file (0.26.0 shipped without this change, and
    its config/esm-hooks and collect/fixture-identity files match
    0.25.0's). 0.24.0 and earlier give a definition no identity; a fixture
    declared through one of them was not run.
  • An import with only a fragment (./support.ts#variant) under a resolver
    registered after the loader that appends ?x=1 or &x=1 to the end of the
    URL text loads as #variant?x=1 first and #variant&x=1 on reload, so its
    worker fixture is set up twice, and so is each worker fixture that names it.
    main keyed by file path and set it up once.
    tsx 4.22.0 does this when a tsx namespace is in use; tsx 4.23.15 and
    import-in-the-middle 3.7.1 insert before the fragment. The docs give the
    workaround (set the parameter through URL.searchParams), a
    worker-fixtures.test.ts row covers it, and a follow-up ticket tracks the
    loader fix.

Issue

ENG-1553, answered as by design. This PR does not change the case ENG-1553
reports: one import URL, a helper that registers beforeEach, and two test
files that compare the fixture's value with the Map they imported. That case
stays as #2148 made it. Each test file reloads the helper and gets a new Map,
while the worker keeps the fixture's value from the first file. The docs now
say so (step 5). Making that case pass would mean evaluating the helper once
and replaying its registrations for each file, which is a separate change.

Verified

Ran it locally: yes

Head 374d6b7f2 adds two commits to a6a14453e. 439edd3e8 corrects the
JSDoc of the keys set in esm-hooks.ts (comment only). 374d6b7f2 adds the
sentence from step 5 to docs/fixtures.mdx, docs/reference/test.mdx, and
both copies of writing-tests.md. On 374d6b7f2, pnpm run build and
pnpm check passed.

ENG-1553's own script on the built CLI, 1 worker, twice each:

BuildENG-1553 scriptThe same helper without beforeEach
main (4fec2de67)Second file failed: fixture and hook module point at different stateBoth tests passed
a6a14453eSecond file failed with the same errorBoth tests passed

Head a6a14453e adds two commits to 300d292a8, which adds six commits on top
of c77e61689. a30df9ca1 merges main (4fec2de67). The merge had one
conflict, the worker fixture list in docs/reference/test.mdx. The list keeps
this PR's bullets and the bullet #2554 added, word for word. Git merged
worker-fixtures.test.ts and both copies of writing-tests.md without a
conflict, and no file under src/ is changed by both. a6a14453e adds one docs
sentence, for the resolver case in the three-build table below.
300d292a8 is
docs-only: it rewraps one paragraph of docs/reference/test.mdx, same words
(pnpm docs:check passed). d14129e7d is
test-only (passing 'junction' to symlinkSync in
open-host-loader.test.ts); that file passed 11 of 11 tests on Node.js
v22.23.5 and v26.13.1. 80a5a47c7, 249ac03d8, and 5ffcda05a are docs-only
(docs/fixtures.mdx, docs/reference/test.mdx, and both copies of
writing-tests.md). The probes, mutation run, and bench-ab table below were
recorded on c77e61689.

112b5e15e changes the fresh-load key from fresh-<cacheKey>-<n> to
<cacheKey>-<n>-fresh, words the older-version notes as 0.25.0 or later, and
adds two tests. Ran on Node.js v26.13.1:

Check5ffcda05a112b5e15e
New open-host-loader test: an older copy imports the config as ?e2e=fresh-module-<n>&e2e-graph=fresh-module-<n> for n 1 to 20, the config is edited, then openHost()Failed: target one (stale module from Node's cache)Passed: target two
New worker-fixtures row: #variant and ?variant=two under a resolver after the loader that sets its parameter through URL.searchParamsNot runPassed, one setup per import; the same row with an appending resolver fails, as the documented limit says
tests/unit/esm-hooks.test.ts, tests/integration/open-host-loader.test.ts, tests/integration/worker-fixtures.test.tsNot rerun105 of 105 passed
pnpm checkPassedPassed

#2554 on main gives a worker fixture a separate instance for each chain
that overrides a fixture it names. This PR names the fixture by the URL its
module was imported by. Both ran together on a6a14453e, on Node.js v26.13.1:

Checka6a14453e
pnpm --filter e2e run test, the whole runner suite6792 tests passed and 15 skipped, in 406 files passed and 1 skipped
pnpm checkPassed
Built CLI, 1 worker, twice from fresh state. A fixture module where client names account and adminTest overrides account, imported as ?tenant=a and ?tenant=b, then as #a and #bFour client setups, one per import and chain. Each test got the client of its own tenant and account
The same module imported by one URL from two test files, and a test that fails once and is retriedTwo client setups, one per chain. The second file and the retry got the instances of the first

With #2554 the resolver limit under "Tradeoffs and limits" reaches the fixtures
that name the affected one. db.mjs declares worker fixture db and a hook.
client.mjs imports it as ./db.mjs#variant and declares client, which names
db and holds a lock file. Two test files import client.mjs, and a resolver
registered after the loader appends apm=1 to the end of the URL of db.mjs.
Each test checks that its client was built from the db it got. Built CLI,
1 worker, twice each from fresh state:

BuildSetupsSecond test
main (4fec2de67)db, clientPassed
300d292a8db, client, dbFailed its own check: it got the second db with the client built from the first
a6a14453edb, client, db, clientFailed with EEXIST on the lock the first client holds
Each of the three, with the resolver using URL.searchParamsdb, clientPassed

Two inputs the newest docs sentences describe ran on 80a5a47c7 with the
built CLI on Node.js v26.13.1, 1 worker, twice each from fresh state:

Casemain (8bad3fc5b)This PR (80a5a47c7)
Two test files import a fixture module that registers a hook as ./support.mjs#variant; a resolver registered after the loader appends ?apm=1 to a URL without a query and &apm=1 to one with a queryOne setup, both tests passedTwo setups (#variant?apm=1, then #variant&apm=1), second test failed with EEXIST
The same, with the resolver adding apm=1 through URL.searchParamsOne setup, both tests passedOne setup, both tests passed
This build runs; the fixture module calls test.extend from the 8bad3fc5b build; imports ?v=one and ?v=twoNot runOne setup, second test received one
The same, with this build's test.extendOne setupTwo setups, both tests passed

Ran the built CLI and two-install runner.js / config/load.js probes on
Node.js v22.23.5 and v26.13.1, pairing this head against a physical
e2e@0.25.0 / main (8bad3fc5b) install:

Casemain (8bad3fc5b / 0.25.0)This PR (c77e61689)
Distinct user queries and fragments (./support.ts?a=1#one vs #two, ?a=a%20b vs ?a=a+b, ??e2e-graph=...)Shared one worker fixture by file pathSeparate worker fixtures
Forwarded import (./forward.mjs?e2e=blue -> ./support.mjs?variant=base) beside ./support.mjs?variant=base&e2e=blueForwarded query treated as loader keySeparate worker fixtures matching Node.js module identity
Retry on entry or queried helper (./support.mjs?variant=blue) when a later resolver appends &x=1, appends literal ?x=1 before #, percent-encodes %652e, or short-circuits retry.e2e.tsOne setup, retry passedOne setup, retry passed on Node.js 22 and 26
Newer loader first, 0.25.0 runner.list() called 3 times on direct registrars, plus helper-wrapped registrarsDirect: a,b each call; helper-wrapped: a, then b, then a,bDirect and helper-wrapped: a,b on all 3 calls
Newer loader first, dynamic import() of a registrar from a plain helper after 0.25.0 runner.list() returnsReturned cached moduleReturned cached module (no COLLECTION_ERROR)
Newer loader first, older openHost() / loadConfigModule({ graph }) across edits, including #instrumented on config/load.js, &x=1 from an earlier resolver, and + rewritten to %20Reloaded edited target each timeReloaded edited target each time
Older loader registered first (originalModuleURLSlot absent) with a fixed-port worker fixture on retryOne setup and one teardownOne setup and one teardown
$ node outside.mjs <head> <0.25.0-build> newer-first
{"listed":["test A","test B"],"result":""}

$ node p2-retry.mjs <head> cand literal '?variant=blue' out.json
{"node":"v26.13.1","exit":0,"status":"flaky","setups":1,"events":["setup ?variant=blue?x=1","teardown ?variant=blue?x=1"]}

$ node graph.mjs <head> <0.25.0-build> before append host
["first","second","third","fourth"]

Mutation testing in an isolated detached worktree (mutant.sh --vitest across
tests/unit/esm-hooks.test.ts, tests/integration/open-host-loader.test.ts,
and tests/integration/worker-fixtures.test.ts): 25 mutants, 25 killed,
0 survived, 0 unconfirmed, 0 did not build, with all 30 added tests killed by
at least one mutant (new-test-kills.mjs exit 0).

No rendered docs screenshot is attached: the docs edits in docs/fixtures.mdx
and docs/reference/test.mdx are prose bullets covering synchronous Node.js
registration (no top-level await), ES module URL query and fragment identity,
the older-loader and older test.extend (0.25.0 or later) file-path fallback, another
resolve hook that changes a module's URL between loads, and Bun test-file
reloads, validated with
pnpm docs:check, and no local Mintlify preview server is configured in this
CLI session. Not measured locally: long-session heap retention and Bun runtime
execution.

bench-ab: 8bad3fc5 (merge base with 8bad3fc5b56e9a9d6e8f273ad2205eb5b5b8db0b) vs c77e6168

apps/testbed: e2e run --workers 1 tests/forms.e2e.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±3% and 10 ms. darwin arm64, Apple M5 Pro x18, Node 26.13.1, load 6.2.

Behavior

Every test ran the same steps with the same outcome on every run of both builds.

Timings

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)1.73 s (1.71 s-1.74 s)1.71 s (1.71 s-1.72 s)-1.2% (-1.5% to +0.2%)same
suite (sum of attempts)831 ms (824 ms-837 ms)829 ms (826 ms-835 ms)-0.7% (-1.5% to +0.8%)same, under the floor
action (events)363 ms (362 ms-363 ms)364 ms (362 ms-364 ms)+0.3% (-0.4% to +1.0%)same, under the floor
step app.open188 ms (186 ms-191 ms)187 ms (186 ms-188 ms)-0.8% (-3.6% to +1.9%)same, under the floor
step locator.fill157 ms (156 ms-157 ms)157 ms (156 ms-158 ms)+0.3% (-0.6% to +1.6%)same, under the floor
step locator.tap90 ms (88 ms-90 ms)90 ms (90 ms-91 ms)+0.6% (0.0% to +4.4%)same, under the floor
step locator.check44 ms (42 ms-45 ms)43 ms (42 ms-44 ms)-1.1% (-4.3% to +5.0%)same, under the floor
step locator.pressSequentially41 ms (41 ms-42 ms)42 ms (42 ms-42 ms)+2.4% (0.0% to +2.4%)same, under the floor
step locator.uncheck32 ms (32 ms-33 ms)32 ms (32 ms-33 ms)0.0% (-3.0% to +3.1%)same
step expect.toBeVisible18 ms (18 ms-18 ms)17 ms (17 ms-18 ms)-5.6% (-5.6% to -2.7%)same, under the floor
step locator.selectOption17 ms (17 ms-18 ms)16 ms (16 ms-17 ms)-5.9% (-13.1% to 0.0%)same, under the floor
step locator.clear17 ms (16 ms-17 ms)17 ms (17 ms-17 ms)+2.9% (-2.8% to +9.7%)same, under the floor
step expect.toHaveValue7 ms (6 ms-9 ms)7 ms (6 ms-7 ms)-5.7% (-18.4% to +18.3%)same, under the floor
step expect.toHaveCount5 ms (5 ms-6 ms)6 ms (5 ms-6 ms)+9.5% (-8.7% to +34.2%)same, under the floor
step expect.toHaveText4 ms (3 ms-4 ms)4 ms (4 ms-5 ms)+29.1% (-13.4% to +41.4%)same, under the floor
step expect.toBeChecked2 ms (2 ms-2 ms)2 ms (2 ms-2 ms)0.0% (-18.4% to 0.0%)same
step expect.not.toBeChecked2 ms (1 ms-2 ms)2 ms (2 ms-2 ms)0.0% (-29.3% to +100.0%)same

Counters

Every counter matched on every run of both builds.

Tests outside the threshold: 0 of 640

With 640 tests compared at 95%, about 32.0 could land outside by chance.

Summary by CodeRabbit

  • Bug Fixes

    • Improved worker-fixture separation and reuse across module reloads and imports with different URL queries or fragments, including during retries.
    • Improved configuration reload behavior so updates are recognized consistently across loader scenarios.
  • Documentation

    • Clarified how worker fixtures behave in Node.js and Bun, including how test-file reloads and top-level await can affect fixture setup.
    • Added guidance on sharing worker fixtures, handling asynchronous setup, and how import URL differences and loader versions affect fixture instances.