This part prepares the environment inside a worker. It sets up global variables and loads the test runner to begin execution.
Columns are parts of the code, grouped by process. Read top to bottom. Solid arrow: hands work over. Dashed: the reply. Purple: crosses between processes. Hook: work inside one part. Dashed boxes repeat. Hover a column name or a step for its full text.
packages/runner/srcpackages/browser/src/client/testerpackages/vitest/src/runtime/runnerspackages/vitest/src/node/poolspackages/vitest/src/runtimepackages/vitest/src/runtime/workersThe test runner executes a single test with all its lifecycle hooks, managing retries and repeats with defaults of zero. It handles setup and cleanup, tracks timing, and inverts results for tests marked as expected failures. Skipped or todo tests return early without running their bodies.
runTest packages/runner/src/run.ts — Runs one test with lifecycle hooks, retries/repeats, cleanup, result timing, expected-failure inversion, and task updates.createBrowserRunner>onBeforeTryTask packages/browser/src/client/tester/runner.ts — Cleans browser user-event state and then invokes inherited attempt preparation.VitestTestRunner.onBeforeTryTask packages/vitest/src/runtime/runners/test.ts — Applies configured mock cleanup, clears the test's snapshot attempt, and resets global assertion state.The pool jobs component submits a job to a virtual machine fork with a communication channel and context. It clears file state, increments the worker ID, and records any termination or cancellation issues. Cleanup always happens, even if errors occur.
VitestTestRunner.onBeforeTryTask packages/vitest/src/runtime/runners/test.ts — Applies configured mock cleanup, clears the test's snapshot attempt, and resets global assertion state.createVmForksPool>runWithFiles>runFiles packages/vitest/src/node/pools/vmForks.ts — Submits a VM fork job with RPC channel and context, records recognized termination/cancellation issues, and always cleans the channel.collect packages/vitest/src/runtime/worker.ts — Dispatches worker execution in collect mode.createBrowserRunner>onBeforeTryTask packages/browser/src/client/tester/runner.ts — Cleans browser user-event state and then invokes inherited attempt preparation.Worker setup dispatches execution in collect mode to gather test information before running. This entry point prepares the worker to receive and process the collection request.
collect packages/vitest/src/runtime/worker.ts — Dispatches worker execution in collect mode.execute packages/vitest/src/runtime/worker.ts — Loads a worker implementation and invokes run or collection with prepared state.The base worker prepares shared caches for modules and execution info, reusing them for efficiency. It deletes invalidated paths and mock variants, then starts the executor to run a batch of tests in parallel.
runBaseTests packages/vitest/src/runtime/workers/base.ts — Prepares shared worker caches and executes a base test batch.startViteNode packages/vitest/src/runtime/workers/base.ts — Lazily creates and caches the base worker's Vitest executor.startVitestExecutor packages/vitest/src/runtime/execute.ts — Returns a Vitest executor wired to worker state, RPC module loading, and error interception.Worker setup creates a Vitest executor wired to worker state with RPC module loading and error interception. It replaces process exit with a diagnostic error and installs error listeners. The initialization awaits environment and spy setup.
startVitestExecutor packages/vitest/src/runtime/execute.ts — Returns a Vitest executor wired to worker state, RPC module loading, and error interception.createVitestExecutor packages/vitest/src/runtime/execute.ts — Creates an executor and initializes Vite environment and spying.Worker setup configures global variables, coverage, and the test environment, then runs or collects files sequentially. It determines isolation for threads and forks with a default of true, resetting module and mock state between files. Coverage stops after all files complete.
run packages/vitest/src/runtime/runBaseTests.ts — Sets up worker globals, coverage, environment, and runner, then runs or collects files with configured isolation and cleanup.runBaseTests packages/vitest/src/runtime/workers/base.ts — Prepares shared worker caches and executes a base test batch.setupGlobalEnv packages/vitest/src/runtime/setup-node.ts — Sets up Node globals, snapshot support and console interception.resolveTestRunner packages/vitest/src/runtime/runners/index.ts — Constructs and prepares a test runner, injecting the executor and RPC-aware lifecycle hooks.Runtime setup constructs the test runner with configuration and injects the executor with lifecycle hooks. It loads diff configuration and serializers concurrently, and wraps callbacks with RPC handling. Bail logic counts previous failures and sends cancellation on test failure.
resolveTestRunner packages/vitest/src/runtime/runners/index.ts — Constructs and prepares a test runner, injecting the executor and RPC-aware lifecycle hooks.The test runner collects and runs test files through runner hooks in order, then flushes task updates. It temporarily wraps cancel to abort running tests with one error and registers fixture cleanup once per runner. Finally, it restores the original cancel method.
startTests packages/runner/src/run.ts — Collects and runs test files through runner hooks, then flushes task updates and returns file tasks.run packages/vitest/src/runtime/runBaseTests.ts — Sets up worker globals, coverage, environment, and runner, then runs or collects files with configured isolation and cleanup.collectTests packages/runner/src/collect.ts — Collects file tasks and applies test-selection modes.resolveTestRunner>onCollectStart packages/vitest/src/runtime/runners/index.ts — onCollectStartThe test runner creates a test context with cancellation, skipping, annotations, and lifecycle hooks. Skip with false does nothing, while other skips mark pending and throw an error. Failure and finish hooks use timeout settings and abort on timeout.
resolveTestRunner>onCollectStart packages/vitest/src/runtime/runners/index.ts — onCollectStartrun packages/vitest/src/runtime/runBaseTests.ts — Sets up worker globals, coverage, environment, and runner, then runs or collects files with configured isolation and cleanup.startTests packages/runner/src/run.ts — Collects and runs test files through runner hooks, then flushes task updates and returns file tasks.createTestContext packages/runner/src/context.ts — Creates the test context with cancellation, skipping, annotations and lifecycle hooks.Runtime setup adds lazy per-test expect access and local-state detection to the test context. The expect object is created on first property access, and optional assertion requirements store a prebuilt error. The same context returns with these additions.
VitestTestRunner.extendTaskContext packages/vitest/src/runtime/runners/test.ts — Adds lazy per-test expect access and local-state detection to a test context and returns it.The test runner executes a suite's hooks and child tasks, recording the final result. Collection failures skip children, while skip and todo modes emit finished without running bodies. Concurrent groups use parallel execution, while others run sequentially with optional shuffling.
runSuite packages/runner/src/run.ts — Runs a suite's hooks and child tasks and records its final result.VitestTestRunner.onAfterRunSuite packages/vitest/src/runtime/runners/test.ts — Records optional heap use, finalizes file snapshots, sends snapshot results, and restores the current parent task.Runtime setup records optional heap usage, finalizes file snapshots, and sends snapshot results through RPC. It marks skipped test snapshot names as checked and restores the current parent task. Failures prevent the final state assignment.
VitestTestRunner.onAfterRunSuite packages/vitest/src/runtime/runners/test.ts — Records optional heap use, finalizes file snapshots, sends snapshot results, and restores the current parent task.Runtime setup executes the hooks that run after all test files complete. This provides a final cleanup opportunity for the test run.
resolveTestRunner>onAfterRunFiles packages/vitest/src/runtime/runners/index.ts — onAfterRunFilesWorker setup runs an asynchronous callback inside a configured test environment, setting environment globals. It awaits setup before the callback and ensures teardown happens in finally, even on failure. Setup failures bypass the teardown step.
withEnv packages/vitest/src/runtime/setup-node.ts — Runs an asynchronous callback inside a configured test environment.Pool jobs submits a virtual machine thread job with path strings and a transferred communication port. It clears file state, sends the pool type with files, and increments the worker ID. Both ports always close in the finally block.
createVmThreadsPool>runWithFiles>runFiles packages/vitest/src/node/pools/vmThreads.ts — Submits a VM thread job with path strings and a transferred RPC port, then always closes both ports.run packages/vitest/src/runtime/worker.ts — Dispatches the worker entry point in run mode.Worker setup dispatches the worker entry point in run mode to execute tests. This entry point prepares the worker to receive and process the run request.
run packages/vitest/src/runtime/worker.ts — Dispatches the worker entry point in run mode.