Skip to content

Isolation and concurrency

Review how the Workers Vitest integration runs your tests, how it isolates tests from each other, and how it imports modules.

Run tests

When you run your tests with the Workers Vitest integration, Vitest will:

  1. Read and evaluate your configuration file using Node.js.
  2. Run any globalSetup files using Node.js.
  3. Collect and sequence test files.
  4. For each Vitest project, depending on its configured isolation and concurrency, start one or more workerd processes, each running one or more Workers.
  5. Run setupFiles and test files in workerd using the appropriate Workers.
  6. Watch for changes and re-run test files using the same Workers if the configuration has not changed.

Isolation model

Storage isolation is per test file. Each test file gets its own storage environment, and any writes to storage during a test file are not visible to other test files. The Workers Vitest integration reuses Workers and their module caches between test runs where possible. A copy of all auxiliary workers exists in each workerd process.

By default, test files run concurrently. To make test files share the same storage (for example, for integration tests that depend on shared state), use the Vitest flags --max-workers=1 --no-isolate.

Modules

Each Worker has its own module cache. As Workers are reused between test runs, their module caches are also reused. Vitest invalidates parts of the module cache at the start of each test run based on changed files.

The Workers Vitest pool works by running code inside a Cloudflare Worker that Vitest would usually run inside a Node.js Worker thread. To make this possible, the pool automatically injects the nodejs_compat, [no_nodejs_compat_v2] and export_commonjs_default compatibility flags. This is the minimal compatibility setup that still allows Vitest to run correctly, but without pulling in polyfills and globals that aren't required. If you already have a Node.js compatibility flag defined in your configuration, Vitest Pool Workers will not try to add those flags.