Skip to content

eslint: require runWithFakedTimers to wrap the whole test body - #342317

Merged
Alexandru Dima (alexdima) merged 5 commits into
mainfrom
agents/vs-code-faked-timers-lint-rule
Oct 17, 2026
Merged

Alexandru Dima (alexdima) merged 5 commits into
mainfrom
agents/vs-code-faked-timers-lint-rule

Conversation

@alexdima

@alexdima Alexandru Dima (alexdima) commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Adds local/code-run-with-faked-timers-whole-test, a lint rule that requires runWithFakedTimers to wrap the whole test body:

test('...', () => runWithFakedTimers({}, async () => {
	// entire test, including setup and assertions
}));

Why

After fn resolves, runWithFakedTimers drains every remaining virtual timer before returning. When only part of a test is wrapped:

  • The drain can fire unrelated far-future timers (e.g. an hourly poll) before the assertions that follow, so the test asserts on state it never meant to produce. Recent example: wrapping only provider.refresh() in defaultAccount.test.ts fired DefaultAccountProvider's hourly poll before the assertions.
  • Objects are created on real timers and then driven on virtual ones, which is hard to reason about.

Survey

Of ~2,260 call sites in src/, ~2,170 already wrap the whole test. 81 sites in 21 files wrap only part of a test.

What the rule allows and reports

Applies to src/vs/**/*.test.ts and only to runWithFakedTimers imported from timeTravelScheduler.js or virtualScheduling/. Import aliases are followed.

Allowed:

  • the call is the whole test body (expression body, or the only return/await statement; test.skip/test.only/conditional callees and leading this.timeout(...) are fine);
  • a wrapper helper whose whole body is the call (function withController(fn) { return runWithFakedTimers({}, ...) });
  • an onHistory capture: inert variable declarations before the call and plain calls (assertions) after it. History is only delivered after the drain, so these assertions have to follow the call.

Reported: partial wraps, calls that are neither awaited nor returned, runWithFakedTimers nested inside another one, and calls from setup/teardown hooks (including from functions nested in a hook).

Rollout

  • The rule is warn, like code-ensure-no-disposables-leak-in-test. Note that warnings fail eslint in CI too.
  • The 18 files with existing violations are in an exclude list (same mechanism as the disposables rule), to be fixed and removed file by file.
  • Legitimate exceptions use eslint-disable-next-line ... -- <reason>. One is included: serverIntegrationTestHelpers.test.ts spawns a real server process, so only stopServer's bounded exit waits are virtualized.
  • Partial wrapping looks intentional in the virtualScheduling self-tests and chatImageReveal, where render() awaits a real image.decode(). The other excluded sites look accidental.
  • Not covered: helpers defined outside *.test.ts. Notably, withTestNotebook (182 callers) creates the notebook editor before its wrap.

How to test

  • cd build && node --test ../.eslint-plugin-local/tests/code-run-with-faked-timers-whole-test.test.ts: 34 passed, 0 failed
  • npm run eslint -- eslint.config.js .eslint-plugin-local/code-run-with-faked-timers-whole-test.ts .eslint-plugin-local/tests/code-run-with-faked-timers-whole-test.test.ts src/vs/editor/contrib/parameterHints/test/browser/parameterHintsModel.test.ts src/vs/platform/agentHost/test/browser/webPubSubRelayTransport.test.ts src/vs/workbench/contrib/notebook/test/browser/cellOperations.test.ts src/vs/workbench/services/accounts/test/browser/defaultAccount.test.ts: passed
  • After merging main: npm run eslint -- eslint.config.js src/vs/platform/agentHost/test/node/serverIntegrationTestHelpers.test.ts src/vs/platform/agentHost/test/node/agentHostGitStateService.test.ts src/vs/workbench/services/accounts/test/browser/defaultAccount.test.ts src/vs/sessions/contrib/chat/test/browser/newChatInput.test.ts src/vs/editor/test/browser/widget/multiDiffEditorWidget.test.ts: passed
  • I ran only the new rule over all 230 *.test.ts files that use the helper. Every flagged site is in an excluded file or has an inline disable.
  • Not run: full npm run eslint and the compile task. The changes are a lint rule, its config, and one comment in a test.

Add `local/code-run-with-faked-timers-whole-test`, which flags
`runWithFakedTimers` calls in `src/vs/**/*.test.ts` that wrap only part
of a test. Code outside the callback runs on real timers, and the drain
after the callback can fire unrelated pending timers (e.g. polls) before
the assertions that follow.

Allowed: the call is the whole test body, a wrapper helper whose whole
body is the call, or an `onHistory` capture (history is delivered only
after the drain). Nested calls and calls from hooks are reported too.

Existing violations (19 files) are excluded and can be fixed file by file.

Co-authored-by: Copilot <[email protected]>
`agentHostGitStateService.test.ts` no longer has partial wraps (#342262).
`serverIntegrationTestHelpers.test.ts` spawns a real server process, so
only the bounded exit waits run in virtual time; suppress inline with a reason.

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 17, 2026 19:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The rule accepts unawaited wrappers and misses wrappers nested inside hook callbacks.

2 open issues
What changed in this PR

Adds an ESLint rule to keep test setup and assertions within runWithFakedTimers, avoiding unexpected timer drains.

Changes:

  • Detects partial wrappers, nested wrappers, and hook usage.
  • Adds rule tests and warning-level configuration with rollout exclusions.
  • Documents one real-process exception.
File Description
src/​vs/​platform/​agentHost/​test/​node/​serverIntegrationTestHelpers.test.ts Adds a justified lint-disable comment; runtime behavior is unchanged.
eslint.config.js Enables the rule with existing-violation exclusions.
.eslint-plugin-local/​tests/​code-run-with-faked-timers-whole-test.test.ts Tests permitted and rejected wrapping patterns.
.eslint-plugin-local/​code-run-with-faked-timers-whole-test.ts Implements wrapper validation and import-alias tracking.

Review effort: Balanced

A bare `runWithFakedTimers(...)` statement was accepted as a whole test
body, although the test can finish before the callback, the drain, and
the timer restore run. Report it as `floating`.

Calls inside a function nested in a setup/teardown hook were accepted
as wrapper helpers; report them as `hook`.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The history-capture exception permits side-effectful destructuring before the fake-timer wrapper.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Reject destructuring defaults that bypass awaited helper validation

.eslint-plugin-local/​code-run-with-faked-timers-whole-test.ts:115

The onHistory exception checks only declaration initializers, so it accepts const [editor = createEditor()] = []; before the awaited helper. The empty array is inert, but the destructuring default executes createEditor() on real timers, bypassing the whole-test requirement. Restrict these declarations to simple identifier bindings, or also validate defaults and computed keys in binding patterns. Add an invalid regression case for this destructuring example.

2 resolved since last review

Review effort: Balanced

…timers-lint-rule

# Conflicts:
#	src/vs/platform/agentHost/test/node/serverIntegrationTestHelpers.test.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants