vitest-pool-fastforks
v0.1.5
Published
A Vitest pool that isolates test files like forks, with a new Node.js environment per file in reused child processes
Maintainers
Readme
vitest-pool-fastforks
A Vitest pool that isolates every test file like the forks pool, and runs faster.
[!WARNING] This pool is experimental. Use it with Vitest's
forkspool as the reference: if a test behaves differently, please report it.
How it works
The forks pool starts a new Node.js process for every test file. This pool starts fewer processes and reuses them, usually one for each worker. In a process, each test file runs in a new Node.js environment: a second instance of Node.js in the same process, with its own global object, module cache, process object, and timers. When the file ends, the pool frees the environment and resets the working directory, the environment variables, and the umask. The next file starts in a new environment.
A new environment is faster to create than a new process, and the process keeps the compiled code of Vitest and of your dependencies.
Requirements
- Node.js
^26.11.0(v26.11.0 or later, but less than v27). Earlier versions cannot run more than one environment in a process (nodejs/node#65977, nodejs/node#66239). The prebuilt addons are only for Node.js 26. - Vitest 4.0.10 or later, before 6.0.
- A prebuilt native addon for your platform: Linux (x64 and arm64, glibc and musl), macOS (arm64 and x64), and Windows (x64).
If one of these is missing, the pool prints a warning and uses Vitest's forks pool instead, so your tests still run.
Usage
npm install --save-dev vitest-pool-fastforks// vitest.config.ts
import { defineConfig } from 'vitest/config'
import { fastForks } from 'vitest-pool-fastforks'
export default defineConfig({
test: {
pool: fastForks(),
},
})Projects with their own config files
If test.projects lists config files, for example projects: ['packages/*/vitest.config.ts'], Vitest does not apply the pool of the root config to those projects. Set pool: fastForks() in each project config file. Inline projects with extends: true use the pool of the root config.
Options
fastForks({
fallback: 'forks',
snapshot: true,
memoryLimit: '10%',
inspector: false,
})| Option | Default | Description |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| fallback | 'forks' | What to do when this pool cannot run (see Requirements). 'forks' prints a warning and uses the forks pool. 'error' throws an error. |
| snapshot | true | Start the child processes from a startup snapshot that has the Node.js built-in modules that Vitest uses. The pool builds the snapshot on the first run (about 0.4 seconds) and keeps it in the temporary directory of the operating system. |
| memoryLimit | The memory of the system, or the memory limit that Node.js reports for the process (process.constrainedMemory()) if it is lower (half of it in watch mode), divided by the number of workers | When a child process uses more memory (RSS) than this after a test file, the pool stops it and starts a new one. Bytes, or a percentage of the memory of the system, such as '10%'. Apart from this limit, the pool also stops a child process when its JavaScript heap, with garbage that the GC did not collect yet, uses more than half of the heap limit of V8 (heap_size_limit). |
| inspector | false | Create each test file's environment with a V8 inspector, so that tests can use node:inspector. Like coverage with the v8 provider, which always turns it on, this turns off the V8 compilation cache, so the run is slower. |
To use the forks pool without changing the config, set the environment variable VITEST_POOL_FASTFORKS=0.
Results
Seven open source projects and one synthetic suite, run with the forks pool and with this pool. Both pools gave the same test results. The only differences were tests that fail at random, and two tests in Vitest's own suite that run only when the pool is named forks.
- Machine: a virtual machine with 16 vCPUs (AMD EPYC-Genoa Processor, no SMT) and 31 GiB of memory, Linux.
- Software: vitest-pool-fastforks 0.1.4, Node.js v26.13.0, and Vitest 5.1.0 (MUI X: 5.0.5).
- Method: the median wall time of 3 runs, in seconds, measured on 2026-11-27.
- Conditions: "16 vCPUs" uses all CPUs. "4 vCPUs" limits the run to 4 CPUs (
taskset -c 0-3), like a common CI runner. Both use Vitest's default number of workers (15 and 3), except readest, whose config setsmaxWorkers: 4.
| Project | Test files | 16 vCPUs: forks → this pool | Speed-up | 4 vCPUs: forks → this pool | Speed-up |
| -------------------------------------------------------------------------------------------------------------- | ---------: | --------------------------: | -------: | -------------------------: | -------: |
| readest | 1,265 | 200.2 → 86.0 | 2.33× | 253.0 → 127.6 | 1.98× |
| Vuetify | 81 | 4.6 → 3.0 | 1.50× | 17.4 → 7.9 | 2.19× |
| Material UI | 467 | 31.2 → 20.1 | 1.55× | 128.1 → 66.1 | 1.94× |
| Base UI | 394 | 41.5 → 29.9 | 1.39× | 194.4 → 136.6 | 1.42× |
| MUI X | 996 | 219.6 → 155.7 | 1.41× | 725.6 → 431.5 | 1.68× |
| Vitest (test/unit) | 217 | 3.8 → 2.8 | 1.39× | 14.3 → 9.1 | 1.57× |
| Instructure UI (Node tests) | 20 | 2.0 → 2.0 | 1.01× | 2.2 → 2.3 | 0.95× |
| js-test-runner-competition (synthetic) | 500 | 15.0 → 5.7 | 2.65× | 63.4 → 21.5 | 2.94× |
The speed-up is largest when a project has many small test files, because then the start of each file takes most of the time. The synthetic suite is an example: js-test-runner-competition (commit fb868f3) generates 500 test files with 10 tests each in fixture/tests/nomock/, and they run with happy-dom.
What stays the same as forks
- Each test file gets new globals, a new module graph, and a new
processobject. - Changes to
process.env, the working directory, and the umask do not leak into the next test file. process.exit()in a test, a crash of the process, and unhandled errors during a test file are reported the same way.process.argv,process.execArgv,process.send, andworker_threads.isMainThreadandthreadIdhave the same values.execArgv(including--requireand--import),env, and the other per-project options.- Watch mode, and coverage with the
v8andistanbulproviders (the same coverage data).
Differences from forks
- Test files that run in the same child process see the same
process.pid. - Modules that you load with
--requireor--importinexecArgvalso run once when a child process starts, before its first test file. --cpu-profand--heap-profwrite one profile per child process, not one per test file.- The test files in a child process share its memory, its garbage collection,
process.uptime(), and the V8 flags that a test sets withv8.setFlagsFromString(). After a test file, the child process runs a full garbage collection if its JavaScript heap and external memory grew by 128 MiB since its start or its last full garbage collection. - Timing can differ from
forks. A garbage collection can come at another point in a test file. A child process keeps the compiled code of earlier test files, so it can hold more than 16 MiB of executable memory. V8 can then compile new regular expressions with fewer optimizations, and a long match can take several times longer. A test that waits a fixed time or compares two timings can fail with this pool. Make such a test wait for the result, or run it in a separate project withpool: 'forks'. - The
exitandbeforeExitlisteners of a test file do not run. Withforks, they run only if the process ends before Vitest stops it. So an error in such a listener can fail some runs withforks, but no run with this pool. For cleanup, useafterAll. - A timer that a test file leaves behind can throw an error after the file ends. Vitest reports the error only until it stops listening to the file. Both pools can miss these errors, and this pool can miss more of them than
forks. To find these timers, run Vitest 4.1 or later with--detectAsyncLeaks. - A worker thread needs more memory to start. Node.js creates each worker thread of a child process from the startup snapshot of the pool, which needs more heap than the built-in snapshot of Node.js. On Node.js 26.13, a worker thread with
resourceLimits.maxOldGenerationSizeMbof 4 or less aborts the child process withFatal JavaScript out of memory, and Vitest reportsWorker exited unexpectedly(withforks, this happens with 2 or less). With a limit from 5 to about 8, the worker thread stops withERR_WORKER_OUT_OF_MEMORY, and withforks, it runs. If a test starts worker threads with such limits, setsnapshot: false, or run the test file in a separate project withpool: 'forks'. - The pool caches the modules in
node_modulesfor all test files in a child process, so later test files do not read them again. It caches only files that did not change after Vitest started, and it clears the cache at each run. A test file can still get an old version of a file in these cases:- The file changes during a run, and a later test file in the same child process loads it.
- The file system's clock is more than 2 seconds behind the system clock.
- Test files see different file systems at one path, for example with a virtual file system.
Limitations
- Coverage with the
v8provider is not faster thanforks. The environments share compiled code, and coverage counts would be wrong, so the pool turns off the V8 compilation cache when thev8provider is on. Theistanbulprovider is not affected. - Debugging (
--inspect,--inspect-brk) uses a new process for each test file, likeforks, because an environment has no debugger connection. - Few, long test files gain little, or run slightly slower. In Instructure UI above (20 files), the run time is mostly the tests themselves.
- Native addons can fail, because a child process runs more than one test file and frees the environment of each file. The
forkspool starts a new process for each file, so it does not have these problems.- An addon that does not support more than one context in a process (an addon that uses
NODE_MODULE()instead ofNODE_MODULE_INIT()) may fail to load in the second test file of a child process. - An addon that does not clean up when its Node.js environment ends, as the Node.js documentation describes, can cause a memory leak or a crash. The child process can keep the memory of each earlier test file (
re21.27.0 and 1.27.1; 1.27.2 fixes this), or crash in a later test file (isolated-vm7.0.1). - An addon that keeps a reference to a JavaScript value in a
staticorthread_localvariable (for example, astatic Napi::FunctionReference) can corrupt memory when a later test file in the same child process loads it (node-av6.0.0 and 7.1.0). The child process then crashes during a test file or after it. This can happen even if the addon works withworker_threads: the environments of a child process run one after another on the same thread. - An addon built with napi-rs (the
napicrate before 3.16.2) can keep state for each thread, so a test file can get the state of an earlier file and fail or crash (@rspack/core2.3.1 withnapi3.10.5 crashes, and the rolldown binding of Vite+ 1.7.0 withnapi3.14.2 fails withFailed to unwrap exclusive reference).napi3.16.2 and later keep this state for each Node.js environment (napi-rs#3689), but they are not tested with this pool yet. - An addon that uses C++ exceptions of node-addon-api, and does not define
NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS, can abort the child process when its async work ends while the pool frees the environment of a test file (bcrypt6.0.0, if a test file does not wait for a hash). The output hasterminate called after throwing an instance of 'Napi::Error'. The results of the file are not lost. To avoid the abort, wait for the async work in the test that starts it. - If an addon causes one of these problems, run the test files that use it in a separate project with
pool: 'forks'. For a memory leak, a lowermemoryLimitalso helps.
- An addon that does not support more than one context in a process (an addon that uses
- The
threads,vmThreads, andvmForkspools are not changed. This pool replaces onlyforks.
Choosing a pool
vmForks and isolate: false can be faster than this pool. They run the test files in a way that differs from forks, and in the projects above, some tests that pass with forks failed with them (for example, 83 and 537 test files in readest). Use this pool if you want the results of forks in less time.
Troubleshooting
- The pool prints
Using the "forks" pool instead. The message tells you which requirement is missing. Usefallback: 'error'to make it an error, for example in CI. - A test that uses
node:inspectorfails. The error isThis Environment was initialized without a V8::Inspector, or a library catches that error and returns an empty result. Setinspector: true. The inspector then also reports the scripts of the earlier test files in the child process. - Every test file fails with
Failed to start fastforks worker, and the output hasSome options passed to require() aren't supported by PnP yet (conditions). Yarn before 4.11.0 (Yarn 3 too) does not work with the pool in Plug'n'Play mode, andfallback: 'forks'does not apply to this error. Update Yarn to 4.11.0 or later, or setnodeLinker: node-modulesin the.yarnrc.ymlfile. To run the tests withforks, setVITEST_POOL_FASTFORKS=0, or runvitest --pool=forksif you setfallback: 'error'. - A test fails only with this pool. Run it with the
forkspool (VITEST_POOL_FASTFORKS=0) to confirm, and report it with the test and the error. A child process exited ... Starting the file in a new child process.A child process stopped before a test file started (for example, it crashed because of an earlier file). The pool runs the file in a new child process, so the result is not lost.- A test file fails with
Worker exited unexpectedly, but passes withforks. An earlier test file in the same child process may have caused the crash, for example through a native addon. See the native addons item in Limitations.
Development
You need Node.js v26.11.0 or later, Python, a C++ compiler (for the native addon), and clang-format 21 (to format the addon). If you use Nix with direnv, run direnv allow to get them from flake.nix.
npm ci
npm run build:js # Build dist/ with vp pack.
npm run build # Build the native addon with node-gyp.
npm run check # Format, lint, and type check with vp check.
npm run check:cc # Check the format of the addon with clang-format.
npm run test:unit # Unit tests with vp test.
npm test # Run each fixture with forks and with this pool, and compare.
node test/watch.mjs # Compare watch mode.
node test/close.mjs # Check that closing Vitest stops the child processes.
node test/addon.mjs # Check the native addon without Vitest.Benchmark
The numbers in Results come from bench/. It needs a Debian machine with 16 CPUs, 32 GiB of memory, and 100 GB of disk, with nothing else running. Everything goes to /data/fastforks (set BENCH to change it).
- Clone this repository on the machine, and check out the commit to measure.
- Run
bash bench/setup.sh. It installs Node.js, builds this package, and sets up each project at a fixed commit. - Run
bash bench/bench.sh. It takes about 6 hours, and writes/data/fastforks/raw/<time>/results.json. It prints a warning if a file fails with only one pool. Check each warning before you use the numbers. - On your computer, copy
results.jsontodocs/results.jsonin a checkout of this repository. Then runnpm ci,npx vp fmt docs/results.json, andnode scripts/results-chart.ts. The script writes the chart and the numbers in the Results section.
Releases
- Open a pull request that sets the version in
package.jsonandpackage-lock.json. The body of the pull request becomes the release note, so write it for users. - Merge the pull request.
release.ymlcreates the tag, andprebuild.ymlbuilds and tests the package on each platform and stages the version on npm. - Approve the
publishjob on GitHub, and then approve the staged version on npmjs.com with 2FA. - Publish the draft GitHub Release that the
releasejob created.
A v* tag cannot be deleted. If the release run fails because of a bug, fix the bug and release the next version. If the tag exists but no prebuild.yml run started, run gh workflow run prebuild.yml --ref v<version> -f platforms=all.
License
MIT
