feat(test): change the running test's timeout with test.setTimeout

Merged
IM
6d ago
+785-103

What and why

  • test.setTimeout(timeout) changes the running test's timeout from its body or a beforeEach hook, 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 beforeAll hook, it changes that hook's own timeout (the config timeout), 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.
  • afterEach and afterAll hooks, test fixture teardowns, and worker fixture teardowns run on cleanupTimeout, so a call there is INVALID_ARGUMENT. So is a value that isn't a positive safe integer up to MAX_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 is COLLECTION_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 one beforeAll hook runs on. Both executors (execute.ts, standalone-test.ts) and SuiteAttempt.within build one.
    • set(ms) moves the AttemptBudget deadline by the difference (shiftDeadline). Operations, --step holds, screen.pause(), and the body race all read that deadline.
    • race(work, onTimeout) replaces the fixed withTimeout and reads the deadline again when it moves or the clock starts.
    • stop(), refill(), and start() stop the clock while a step is held or paused. AttemptHolds only calls them.
    • Once the race settles, set returns false, so code left running cannot move the timeout or the watchdog.
  • The published attempt gets a setTimeout hook beside expectFailure. A beforeAll hook is named through a globalThis slot (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 mcp test 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 a beforeEach; lowered below the time spent; a raised and a lowered beforeAll; a beforeAll on a worker-isolation engine whose device calls get the raised budget; refusals from afterEach, 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 an e2e mcp test run.
  • tests/integration/open-host-deadline.test.ts: the hosted-session message.
  • tests/unit/step-hold.test.ts: AttemptTimeout lowered below the time spent, raised, moved while a step is held or screen.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 in set, the watchdog announcement, the e2e mcp hook, the ended guard, the beforeAll timeout on the attempt's budget, or the inSuiteHook wrapper 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/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it. Japanese and Chinese pages carry the new section and migration rows.
  • 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.