feat(test): change the running test's timeout with test.setTimeout
Merged
IM
6d ago+785+785+012345678901234567890123456789-103-103-012345678901234567890123456789
What and why
test.setTimeout(timeout)changes the running test's timeout from its body or abeforeEachhook, as Playwright's does. The new timeout counts from the start of the attempt, like the{ timeout }option it replaces for that attempt. A retry starts from the option again.- A timeout shorter than the time already spent times the test out at once.
- In a
beforeAllhook, it changes that hook's own timeout (the configtimeout), counted from the hook's start. The tests keep their own. The worker fixtures the hook sets up and the device calls it makes run on the changed timeout too. afterEachandafterAllhooks, test fixture teardowns, and worker fixture teardowns run oncleanupTimeout, so a call there isINVALID_ARGUMENT. So is a value that isn't a positive safe integer up toMAX_TIMER_MS(the option's own rule), a call from a hosted session (host.openSession), and a call from code that a test or hook left running after it ended. A call outside a running test or hook isCOLLECTION_ERROR, pointing at the option.
test('imports a large file', async ({ app }) => {
test.setTimeout(180_000);
await app.open('/import');
});
How it works:
AttemptTimeout(packages/core/src/run/budget.ts) owns the timeout one attempt or onebeforeAllhook runs on. Both executors (execute.ts,standalone-test.ts) andSuiteAttempt.withinbuild one.set(ms)moves theAttemptBudgetdeadline by the difference (shiftDeadline). Operations,--stepholds,screen.pause(), and the body race all read that deadline.race(work, onTimeout)replaces the fixedwithTimeoutand reads the deadline again when it moves or the clock starts.stop(),refill(), andstart()stop the clock while a step is held or paused.AttemptHoldsonly calls them.- Once the race settles,
setreturnsfalse, so code left running cannot move the timeout or the watchdog.
- The published attempt gets a
setTimeouthook besideexpectFailure. AbeforeAllhook is named through aglobalThisslot (inSuiteHook,packages/core/src/internal/test-timeout.ts). - The worker announces the new timeout and what is left, so the scheduler's watchdog follows it and names it if it kills a blocked worker.
e2e mcptest runs take it too.
Issue
Part of #1855, item test.setTimeout(60_000) under Test runner. @okwasniewski
Verified
Ran it locally: yes, through the real runner (in-process, worker processes, and e2e mcp).
tests/integration/test-set-timeout.test.ts: raised and lowered from a body and abeforeEach; lowered below the time spent; a raised and a loweredbeforeAll; abeforeAllon aworker-isolation engine whose device calls get the raised budget; refusals fromafterEach,afterAll, a worker fixture teardown, bad values, and a call at collection; and a worker that blocks its event loop after a raise, which the watchdog kills past the new timeout.tests/integration/mcp-test-run.test.ts: a lowered and a raised timeout in ane2e mcptest run.tests/integration/open-host-deadline.test.ts: the hosted-session message.tests/unit/step-hold.test.ts:AttemptTimeoutlowered below the time spent, raised, moved while a step is held orscreen.pause()waits, the release that announces it, and the refusal once the race settled.- Mutation checks, each on rebuilt
dist: removing the deadline re-check inset, the watchdog announcement, thee2e mcphook, theendedguard, thebeforeAlltimeout on the attempt's budget, or theinSuiteHookwrapper on worker fixture teardowns fails a test above.
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/**/*.mdxpage for the behavior, andskills/e2e/if the skill describes it. Japanese and Chinese pages carry the new section and migration rows. - Contract change: emitted
.d.tsreviewed,tests/types/sdk-types.tsupdated; wire change edits the schema and both fixtures. - Engine contract change (
e2e/engine): changesets fore2e,@e2e-dev/web, and@e2e-dev/mobile.