Skip to content
useblacksmithPublic

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

@useblacksmith/vitest

Speed up vitest execution by only running tests impacted by a change.

Beta The plugin is under active development; options and the record format may change between releases. Please report any issues in the GitHub repository

Usage

npm install --save-dev @useblacksmith/vitest
// vitest.config.ts
import { defineConfig } from 'vitest/config'
import blacksmith from '@useblacksmith/vitest'

export default defineConfig({
	plugins: [blacksmith()],
})

With the plugin, vitest only runs tests impacted by changes since the last successful run.

Requires Vitest 4.1.1 or newer.

Vite+

Vite+ runs Vitest under vp test, so the plugin is loaded from the shared vite.config.ts:

// vite.config.ts
import 'vite-plus/test/config'
import { defineConfig } from 'vite-plus'
import blacksmith from '@useblacksmith/vitest'

export default defineConfig({
	plugins: [blacksmith()],
})

vp test run then runs the affected selection, including with test file filters and --config <path> --dir . from a workspace package. Requires vite-plus 1.5.0 or newer, the first release that ships Vitest 5.1.

Bun

Install with bun add --dev @useblacksmith/vitest; the config is the same.

Command What runs Vitest What to know
bun run test, bunx vitest run Node Nothing more is needed.
bun --bun vitest run, bun --bun run test Bun The plugin also records what a test reaches through Bun's own file, process, shell and worker APIs.

Do not use bun test: that is Bun's own test runner, which does not load Vitest or the plugin. Bun and Node keep separate records, so the first run after switching between them runs every test. The plugin is tested on Bun 1.3 and 1.4; see Caveats for what differs under Bun.

How it works

The plugin uses Vitest's dependency tracking: each run records which modules and files every test depends on, and the next run compares the checkout against that record to calculate the impacted test set; a test the last successful run did not cover always runs.

Each run ends with a summary of what it ran and skipped and how it ended, then the same as JSON for whatever collects the job's logs:

[blacksmith] summary: passed; ran 28 of 540 test files, skipped 512 unaffected; tests: 310 passed, 0 failed, 2 skipped; dependency record restored, saved; plugin took 1.2s
[blacksmith] run {"v":1,"plugin":"@useblacksmith/vitest","version":"1.0.0","runner":"vitest","outcome":"passed","files":{"total":540,"affected":28,"skipped":512,"ran":28,"failed":0},...}

On a Blacksmith runner the plugin also sends that JSON to the runner, with the repository's id, so Blacksmith can show what test selection saved. It sends nothing else, does not wait for an answer, and a report that is not taken changes nothing about the run.

Options

option default effect
runAllTests false Run every test instead of the affected selection.
cacheKey none Metadata used for skipping is keyed by working directory, test configuration and runtime (runtime version, OS, architecture). Set this if you need additional granularity, e.g. a job matrix that runs the same suite against different databases, services or environment variables.
dependencies {} Dependencies the tracking cannot see, as globs of test files mapped to the glob(s) of the files they depend on, relative to the config root (../shared/** reaches outside a package): { 'tests/**/*.spec.ts': 'fixtures/**' }. A change to a matching file reruns the mapped tests, git-ignored files (generated code, build output) included; a glob that matches no file in the repository warns.

Narrowing

A change to a file does not rerun every test file that loaded it. The plugin records, for each test file, which top-level functions it entered in every module it loaded, and looks at what a change actually touched:

What changed in the file Test files that rerun
The body of a top-level function (function f() {}, const f = () => {}) Those that called the function
Another top-level declaration (a constant, a class, an enum), or a function's parameter list Those that loaded a module importing it by name, through re-exports too. A namespace import, a dynamic import() or a mock of the module counts as importing every name
One key of an object literal held in a const (const FLAGS = { A: 'a', B: 'b' }): changed, added or removed Those that called a function reading that key as FLAGS.A, or using the object in any other way (FLAGS[name], Object.values(FLAGS), passing it on), in the file or in a module importing it by name. Those that loaded a module doing so outside a function, too. Any other object counts as one declaration: one with a spread, a computed key, an accessor, a method or a function value
Types, comments, or a new declaration nothing uses yet None. A JSX pragma comment (@jsx, @jsxFrag, @jsxRuntime, @jsxImportSource) is code: a change to one reruns every test file that loaded the file. So is any comment in a file imported with ?raw, for the test files that import it that way. With verbatimModuleSyntax in the nearest tsconfig.json (or one it extends, or the project it refers to that holds the file), an import counts as a type only where it is written import type. A type annotation inside a class that has a decorator is not a type here: emitted decorator metadata makes it a value, so it counts as a change to the class
A new import Those that had not loaded the imported module
Any other top-level statement, an export *, or declarations that changed places All that loaded the file

A value built from a changed one counts as changed, and a function that mentions a changed value reruns the test files that called it. Files the plugin cannot take apart (CommonJS, files using eval, anything that is not JavaScript or TypeScript) and test files run in browser mode rerun for any change to a file they loaded, as does a run with sequence.hooks set to anything but 'stack'.

To record function entries the plugin adds one call at the start of each top-level function while Vite transforms the module; source maps account for it. The call names nothing of the module's and does nothing where the plugin is not recording, so a function turned into text with toString() still runs wherever that text is evaluated. The record is kept beside the dependency record and travels with it. It holds file names and hashes, not source: every function, constant, object key and export name is stored as a hash salted with the repository's name, as is every hash of code.

What narrowing cannot see:

  • A file added where an import.meta.glob pattern that starts at the project root or with an alias looks, or one that git ignores. For a pattern relative to the module (./pages/*.ts), or to a base relative to it, a file the pattern matches that is added or removed reruns the test files that loaded the module; a file it does not match reruns nothing.
  • A test whose result depends on a function's source text or length rather than on calling it.
  • A test whose result depends on where code sits in its file, such as a line number read from a stack trace: a comment or a declaration added above a function moves it without changing it.
  • A test whose result depends on the order two modules load in, where both do something as they load: an import added to or removed from a file can move a module the test file also loads another way, and that alone reruns nothing.

A changed top-level value that calls something as it is evaluated (a call, new, a tagged template, an assignment, a decorator, a class's static initializer) reruns every test file that loaded the module, since the call may have done more than make the value. So does an unchanged value built from a changed one. A value of literals, objects, arrays, functions and other names reruns only the test files that loaded a module importing it. So does a value whose calls are shown to change nothing but what they make: the language's own reads (Object.values, filter, map, new Set and the like) of consts of literals declared in the same file, with callbacks and functions of the same file that write only to their own variables and to arrays they made themselves, and that call nothing else. Anything that cannot be shown this way (an imported function, a method of an unknown value, a loop that may not end, a const declared before some other call ran) counts as a call that may have done more.

Caveats

  • Only dependencies the test reaches through node are tracked. import, require, and fs are tracked automatically, in the test process, in every node process it starts through child_process and in every worker_threads worker (each gets a preloaded recorder; their own children inherit it). A child or worker records what it has loaded as it goes, so one the test kills or terminates still counts, up to the end of its test file. For any child, the command and the arguments that name files are recorded too. A child started with env: { ..., NODE_OPTIONS: undefined }, or a worker with env: SHARE_ENV, gets no recorder, only its command line. A file the test, or a node child it started, wrote before reading it is the test's own output and is not tracked (a git-ignored directory the test copies into and reads back, say); what it was produced from is. A file a non-node program wrote and the test then read, or a module the test wrote and then imported, is still tracked, and reruns the test on every fresh checkout where it is absent. Files read by globalSetup, by non-node programs, or build outputs a child runs in place of their sources are not; declare them with the dependencies option. In browser mode, vitest itself reports nothing, so the plugin records the modules the browser loaded from the project's Vite module graph instead; files a browser test fetches at run time are not tracked.
  • Installed packages are compared by version. The plugin records the packages each test file loaded, transitive ones included, with the version in their package.json. After an install, a test file reruns when one of its packages has another version or is gone; a lockfile change alone reruns nothing. A package.json change reruns the test files below it only when the fields modules resolve by change (exports, imports, type, main, name); a change to anything else in it reruns only the test files that read the file themselves or that a dependencies rule maps it to. With pnpm, which links each name to one of the versions it keeps side by side, a package also counts as changed when a link that led to it now leads to another version. With Yarn and npm workspaces, which hoist one copy of a package and install another version inside the workspace that asks for it, a copy installed later in front of the one a test file loaded counts as a change too. A new version of Vitest, of a package a config or globalSetup file imports, or of anything those declare as a dependency runs every test. Not seen: a package changed in place at the same version (patch-package, a git or file: dependency) and a package installed outside the repository. Browser-mode test files have no package list and rerun on any lockfile change, as does every test file on a Node without module.registerHooks (before 22.15), which cannot see what an ES module package loads.
  • Bun. bun run test runs Vitest on Node and needs nothing more. Under the Bun runtime (bun --bun vitest run), the plugin also records what a test reaches through Bun.file, Bun.write, Bun.spawn, Bun.spawnSync, the $ shell and Worker. Bun does not tell which package loaded which, so a test file's package list there is every package its worker had loaded. With the default isolate: true each test file has a worker of its own and the list is exact; only with isolate: false, where test files share a worker, can an install rerun test files that did not load the package. The same holds for a file of your project loaded with require or a native import: it counts for every test file its worker runs after loading it. For the $ shell, the test worker's own process.env carries the recorder there (NODE_OPTIONS, BUN_OPTIONS and BLACKSMITH_CHILD_RECORD). Bun and Node keep separate records.
  • Jobs that run the same suite share its records. A test file's record is kept per config root, config file, set of projects and runtime, not per workflow job or shard, so a test file that passed in one job is skipped in another job running the same suite. Jobs that run the same suite under a different env or different services are not told apart unless each has a cacheKey of its own.
  • A run with coverage runs every test. With --changed, Vitest measures coverage for the changed files alone, so thresholds would pass without covering anything. The plugin logs a warning and selects nothing. A run that fails only its coverage thresholds vouches for no test.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors