bivium 0.5.1 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,47 +1,12 @@
1
1
  # bivium
2
2
 
3
- > Run only the tests a change can affect, and never miss one.
3
+ Run only the tests your change can reach.
4
4
 
5
- bivium builds a static, file-level dependency graph of a repository
6
- and walks it backwards from the files a change touches to the tests that
7
- reach them.
8
- It fails closed: anything it can't read
9
- (a non-literal `import()`, an unresolved specifier, a disk read with no
10
- declared inputs) counts as depending on everything,
11
- and a change it can't place runs the whole suite, with a reason.
12
- It picks the tests; your runner runs them.
13
-
14
- It has no dependencies and needs no install of the repository it plans:
15
- it reads the git index and the working tree, never `node_modules`.
16
- CI can plan in a job that skips the install,
17
- and skip the test jobs whose selection is `none`.
18
- It runs on Node ≥ 22 and Bun.
19
- It was extracted from
20
- [carbon-components-svelte](https://github.com/carbon-design-system/carbon-components-svelte)'s
21
- `test:changed`.
22
-
23
- ```sh
24
- bun add -d bivium
25
- ```
26
-
27
- ```ts
28
- // bivium.config.ts
29
- import { defineConfig } from "bivium";
30
-
31
- export default defineConfig({
32
- suites: {
33
- unit: { tests: "tests/**/*.test.ts", run: "vitest run {files}", root: "tests" },
34
- e2e: {
35
- tests: "e2e/**/*.test.ts",
36
- run: "playwright test {files}",
37
- root: "e2e",
38
- urls: { "/*": "e2e/fixtures/*" }, // page.goto("/x.html") → e2e/fixtures/x.html
39
- },
40
- },
41
- roots: ["src", "tests", "e2e"], // a changed file outside them runs everything
42
- ignore: ["**/*.md", "docs/**"],
43
- });
44
- ```
5
+ bivium reads your code's imports to work out which tests a change could
6
+ break, and hands those to your test runner: Vitest, Jest, Playwright,
7
+ `bun test`, or any other.
8
+ When it can't tell what a change touches, it runs everything,
9
+ and says why.
45
10
 
46
11
  ```console
47
12
  $ bunx bivium plan
@@ -50,318 +15,11 @@ unit tests: 3 of 12 files
50
15
  tests/Modal.test.ts
51
16
  tests/css/unknown.test.ts
52
17
  e2e tests: none
53
- $ bunx bivium run --suite unit # vitest run tests/Button.test.ts …
54
18
  ```
55
19
 
56
- ## What it reads
57
-
58
- A one-pass lexer reads JavaScript and TypeScript
59
- (`.js`, `.ts`, `.jsx`, `.tsx`, and their `m`/`c` variants),
60
- the scripts and markup expressions of `.svelte`, the scripts of `.vue`,
61
- `.astro` and `.html`, and the `script[src]`,
62
- `link[href]` and `iframe[src]` of `.html`.
63
- Only code counts: an import spelled in a string
64
- or a comment adds no edge.
65
-
66
- | It follows | As |
67
- |:---|:---|
68
- | Static, side-effect and dynamic imports, `export … from`, `require()` | Edges to every file the specifier can load: each extension that exists, each `exports` condition. A missing `x.js` (`.jsx`, `.mjs`, `.cjs`) is its TypeScript source, as TypeScript reads it: `x.ts`, `x.tsx` or `x.d.ts`, through any path, alias or `exports` target. A value import that finds only `x.d.ts` is unknown: what runs isn't in the graph |
69
- | `vi.mock()`, `vi.importActual()` and the rest of the family | Edges |
70
- | `import.meta.glob()` | Every matching file, now and later |
71
- | JSDoc `@type {import("x")}` and `@import` | Edges (TypeScript reads them) |
72
- | `%sveltekit.assets%/x` in a SvelteKit `app.html` | An edge to `static/x` in the project, the nearest directory with a `svelte.config.*` or a `package.json`. `%sveltekit.head%`, `%sveltekit.body%` and the rest aren't references |
73
- | `page.goto("/x.html")` in a suite with `urls` | An edge to the page |
74
- | `compilerOptions.paths` and `baseUrl` of the nearest `tsconfig.json`, with its relative `extends` | Edges to every target that exists, tried after `alias` and before `package.json` `exports`. A `tsconfig.json` that doesn't parse maps nothing, and `check` lists it |
75
- | `#name` subpath imports, through the `imports` of the importing file's own `package.json` | Edges to every condition's target that exists; an unmatched `#name` is unknown |
76
- | Named imports from a module made only of re-exports | An edge to the one file that exports each name, when the package declares `sideEffects` |
77
- | A file's own package and its ancestors' `package.json` dependencies, and builtins | `pkg:` nodes |
78
- | Anything else | Unknown: the file depends on every change |
79
-
80
- A root `bun.lock`, `package-lock.json` or `pnpm-lock.yaml` isn't a
81
- source file: a change to one seeds the packages it changed
82
- (see [Lockfiles](#lockfiles)).
83
-
84
- Nor is a `tsconfig.json` (or a file one `extends`)
85
- or a `package.json` under `roots`: their options, `paths`,
86
- `type` or `exports` change how the files under them resolve or run,
87
- which no import says.
88
- A change to one seeds the files it governs,
89
- those it's the nearest of its kind to.
90
- A change the parsed file doesn't show (formatting, comments),
91
- or to a `package.json`'s metadata alone
92
- (`version`, `description`, `repository` and the like), seeds nothing,
93
- and a dependency change is the lockfile's.
94
-
95
- A file it can't lex cleanly is read by a regex extractor instead,
96
- which over-approximates.
97
- CSS isn't scanned: generated stylesheets reach their inputs through
98
- `edges`, and a [narrower](#narrowers) can do better.
99
-
100
- ## Config
101
-
102
- `bivium.config.ts` (or `.mts`, `.mjs`, `.js`, `.json`) in the repository
103
- root, or `--config <file>`.
104
- A `.ts` config needs Bun or Node ≥ 22.18.
105
- Every glob key takes one glob or a list.
106
- An unknown key is an error, since a misspelled one would quietly select
107
- less.
108
-
109
- | Key | Meaning |
110
- |:---|:---|
111
- | `suites.<name>.tests` | The suite's test files |
112
- | `suites.<name>.run` | The command `bivium run` runs. `{files}` is replaced by the selection (nothing for `all`); without it, the files are appended |
113
- | `suites.<name>.root` | Where the orphan rule applies: a changed file here that no test of the suite reaches is wired some other way (a setup file, a runner config), so the whole suite runs. Paths, not globs (`e2e`, not `e2e/**`): a glob is an error. Default: the whole repository |
114
- | `suites.<name>.urls` | URL pattern → file pattern, for `page.goto()` calls under `root`. A list names the page first, then what else it loads that no import says, such as a router's layouts: `{ "/*": ["app/(site)/*/page.tsx", "app/(site)/layout.tsx", "app/layout.tsx"] }`. The page must exist; the rest are edges where they do |
115
- | `suites.<name>.pages` | Pages only a `goto()` opens; files only they load are wired to no test |
116
- | `suites.<name>.setup` | Files the runner loads before every test (Vitest and Jest `setupFiles`, Mocha `--require`, Playwright `globalSetup`): every test of the suite depends on them and what they import. bivium doesn't read the runner's config, so name them here |
117
- | `suites.<name>.wiring` | Files whose change runs the whole suite: another harness, a runner config |
118
- | `suites.<name>.nonInputs` | Files under `root` the runner never reads, such as type-check inputs |
119
- | `suites.<name>.types` | `false` when the suite runs code and can't observe types (Vitest, Jest, Playwright, `bun test`): a change that reaches its tests only through `import type`, `export type … from`, JSDoc types or a declaration file selects none of them. For the orphan rule a type-only reach counts only for a declaration file, since the runner may load any other file by name: list a types-only module under `root` in `nonInputs`. Default: `true`, which a type-checking suite (`tsc`, `vitest --typecheck`) needs. An inline `import { type A }` is always an edge: under `verbatimModuleSyntax` it still loads the module |
120
- | `suites.<name>.output` | The step output `plan --github` writes. Default: `<name>-tests` |
121
- | `roots` | Paths (not globs) the graph reads. A changed file outside them that isn't ignored runs everything. Default: the whole repository |
122
- | `ignore` | Files no test can observe except through an edge (docs, stories). A change to one selects only the tests that reach it, never runs a suite by the orphan rule, and doesn't select the tests whose dependencies are unknown |
123
- | `alias` | Specifier prefix → path, as the bundler aliases it. Tried before tsconfig `paths` and `package.json` `exports`. A key ending in `/` is the prefix, as Vite reads it: `{ "@/": "src/" }` maps `@/x` to `src/x` |
124
- | `edges` | Generated file glob → its inputs, which it depends on transitively, as with `@depends-on`: `{ "dist/*.css": "styles/**" }`. A key may name files git doesn't list, such as a gitignored generator output (`{ ".velite/*": "content/**" }`): an import that resolves under it, by extension or as an index, lands on that file instead of being unresolved. In a value, `$1`, `$2`… are what the key's wildcards matched, counted from the left: `{ "examples/*/package.json": "examples/$1/**" }` |
125
- | `reads` | File glob → the files it reads as text, as with `@reads`: only a direct change to one counts, so their imports and unknowns don't spread to it. An `edges` or `reads` key that names one file (no wildcard) declares what it reads, as its own `@depends-on` or `@reads` would, so its disk reads aren't unknown. Values take `$1`… as in `edges`: `{ "tests/*.test.ts": "tests/__snapshots__/$1.test.ts.snap" }` |
126
- | `self` | The selector's own files: a change to one runs everything. The config file is always one |
127
- | `importOnly` | Files only an import reaches: deleting one never orphans a suite |
128
- | `hermetic` | Files whose disk reads and non-literal imports reach no file of the repository: a library's `fs` calls on its caller's paths, a test that reads only the temporary files it writes. They add no unknown, and `check` still flags a repository path one spells that nothing declares |
129
- | `barrels` | Modules that may bind named imports. Default: every module made only of re-exports |
130
- | `external` | Packages a dependency installs that your code imports without declaring |
131
- | `inert` | Packages no test uses except by importing them: linters, formatters, type packages (`["@biomejs/biome", "knip", "@types/*"]`). A [lockfile change](#lockfiles) that only they reach selects nothing. With `loadedByName` set, only packages with a `bin` need listing. bivium itself always is |
132
- | `loadedByName` | Packages a test may load other than by an import: a test environment or a plugin a config names by string (`["jsdom", "ts-jest"]`). Setting it, even to `[]`, says every other package without a `bin` is loaded only by imports, so a [lockfile change](#lockfiles) that reaches one no test imports selects only its importers. A package with a `bin` always counts as one a test may run |
133
- | `extensions` | Suffixes tried after a specifier |
134
- | `deadGlobs` | Dead globs `check` allows, each `{ file, glob, reason }`, the glob as `file` spells it |
135
- | `knownUnknowns` | Nodes `check` allows to have unknown dependencies, each `{ file, reason }`. With it set, even to `[]` (which is `--max-unknown 0`), `check` fails on an unknown node some test reaches that it doesn't list, and on an entry that's no longer unknown, so a new unknown can't replace one that went away |
136
- | `narrowers` | See [Narrowers](#narrowers) |
137
- | `base` | The default base ref. Otherwise the upstream default branch (`origin/HEAD`, then `origin/main`, `origin/master`, `main`, `master`) |
138
- | `cache` | The cache's directory, or `false`. Default: `node_modules/.cache/bivium`, or `.bivium/cache` |
139
-
140
- ## A change no test reaches
141
-
142
- A changed file under a suite's `root` that no test of the suite reaches
143
- is wired some other way (a runner config, a setup file),
144
- so the whole suite runs: the orphan rule.
145
- With the default `root`, the whole repository,
146
- any file bivium can't trace to a test runs the suite,
147
- which is sound and coarse.
148
- Set `root` on every suite to the directories its tests
149
- and sources live in, and say what the rest is:
150
-
151
- | You changed… | and want… | Use |
152
- |:---|:---|:---|
153
- | a file no test can observe: docs, stories, a changelog | nothing to run, unless some test imports it | `ignore` |
154
- | a file under the suite's `root` only another tool reads: type-check inputs | nothing to run for this suite | the suite's `nonInputs` |
155
- | a runner config or a second harness | the whole suite | the suite's `wiring` |
156
- | a setup file the runner loads before every test, or what it imports | every test of the suite | the suite's `setup` |
157
- | the selector's config, or its own code | everything | `self` (the config file always is) |
158
- | a declaration file, in a suite that runs code | nothing | the suite's `types: false` |
159
- | a types-only module only `import type` reaches, in a suite that runs code | nothing | the suite's `types: false`, and the module in its `nonInputs` |
160
- | a file a test lists but doesn't read (a route, a story) | the test, when one is added or deleted | `// @lists <glob>` in the test |
161
- | a file a build turns into what tests load | the tests that load the output | `edges` from the output to its inputs |
162
-
163
- A file outside `roots` that isn't ignored runs everything:
164
- it's the backstop for wiring the graph can't see, such as a CI workflow.
165
-
166
- `bivium check` lists every file under a suite's `root` that no test
167
- reaches and the config doesn't exempt,
168
- with a fix for each by what it looks like: a test's snapshot,
169
- a runner's config, or anything else.
170
- It's the list to work through when adopting bivium,
171
- and a new entry is a gap someone added.
172
-
173
- A file's own disk reads make it unknown,
174
- but a test that hands a path to the code under test
175
- (`typecheck({ workspace: "tests/fixtures" })`),
176
- or a `hermetic` one that reads it, reads nothing the graph can see.
177
- So `check` also reads the string literals in each test,
178
- and in what the tests reach under a suite's `root`,
179
- for repository paths: relative to the root when they have a slash
180
- (`join(root, "tests/fixtures")`), and relative to the file
181
- (`new URL("fixtures", import.meta.url)`).
182
- It fails on one a change to which wouldn't select the tests that reach
183
- the file, with the `reads`, `edges` or annotation that declares it.
184
- A path whose change runs the suite anyway, as `wiring`,
185
- `self` or by the orphan rule, doesn't fail,
186
- and an unreached file's fix names where it's spelled.
187
- Strings inside brackets are skipped as data
188
- (`["src/a.ts", …]`, `map["key"]`), as are the directories a file is in
189
- and paths outside the graph.
190
- A path a test only names, such as an expected output,
191
- takes `// @lists <path>`.
192
-
193
- ## Recipes
194
-
195
- **Vitest or Jest.**
196
- Name the setup files the runner loads,
197
- mirror `resolve.alias` (a Vite key like `"@/"` works as written),
198
- give snapshots to their tests, and, for a suite that runs code,
199
- skip type-only edges:
200
-
201
- ```ts
202
- suites: {
203
- unit: {
204
- tests: "src/**/*.test.{ts,tsx}",
205
- run: "vitest run {files}",
206
- root: "src",
207
- setup: ["src/setupTests.ts"], // vitest.config.ts `setupFiles`
208
- wiring: ["vitest.config.ts"],
209
- types: false,
210
- nonInputs: ["src/**/types.ts"], // types only: nothing loads them at runtime
211
- },
212
- },
213
- alias: { "@/": "src/" },
214
- reads: { "src/**/*.test.{ts,tsx}": "src/$1__snapshots__/$2.test.$3.snap" },
215
- ```
216
-
217
- tsconfig `paths` (what `vite-tsconfig-paths` reads) are read without an
218
- `alias`.
219
- An extensionless `vi.mock("@/x")` resolves as an import does.
220
- A test environment the runner loads by name (`jsdom`, `happy-dom`) goes
221
- in `loadedByName` if you set it.
222
-
223
- **pnpm workspaces.**
224
- Workspace packages resolve through their `package.json` `exports`,
225
- and a `.js` specifier into one (`@acme/ui/Button.js`) reaches its `.ts`
226
- source.
227
- A `pnpm-lock.yaml` change selects through the packages it changed.
228
- Set `roots` to the packages and apps whose changes the suites need to
229
- see, and a suite per test runner.
230
-
231
- **Playwright.**
232
- Map `page.goto()` URLs to the pages they open with `urls`,
233
- list pages only a `goto()` opens in `pages`,
234
- and name `playwright.config.ts` in `wiring`:
235
-
236
- ```ts
237
- e2e: {
238
- tests: "e2e/**/*.spec.ts",
239
- root: "e2e",
240
- urls: { "/*": "e2e/fixtures/*.html" },
241
- wiring: ["playwright.config.ts"],
242
- },
243
- ```
244
-
245
- **Generated code.**
246
- A content or type generator that writes into a gitignored directory
247
- (`.velite`, `.contentlayer/generated`) is an `edges` entry from what it
248
- writes to what it reads:
249
- `{ ".velite/*": ["content/**", "velite.config.ts"] }`.
250
- An import of `#velite` or `../.velite` then reaches the content,
251
- and not every change.
252
-
253
- ## Lockfiles
254
-
255
- A dependency bump changes a lockfile, which no file imports.
256
- When a root `bun.lock`, `package-lock.json` (v2 or v3)
257
- or `pnpm-lock.yaml` (v6 or v9) changes,
258
- bivium diffs it against the merge base and seeds the walk with the
259
- `pkg:` nodes of the packages whose entries changed,
260
- and of every package that depends on one,
261
- so a `svelte` bump selects what imports `svelte`,
262
- or a package built on it.
263
- A `package.json` whose only change is to dependencies the lockfile
264
- records the same way counts as those packages changing.
265
-
266
- A changed package can also be used in ways the graph can't see:
267
- a CLI a test spawns, a runner, a type package.
268
- So when a change reaches a package that a `package.json` declares,
269
- or that some file imports, and that no test imports, everything runs,
270
- with the reason
271
- (`bun.lock: playwright 1.63.0 → 1.64.0, and no test imports it`),
272
- unless the package is `inert`
273
- (bivium itself always is: a bump changes which tests later plans pick,
274
- not what a test does).
275
- With `loadedByName` set, only a package with a `bin` in the lockfile,
276
- or one it lists, counts as used that way:
277
- a bump that reaches a library only a CMS config imports selects nothing.
278
- Everything also runs for a lockfile bivium can't parse,
279
- one whose settings other than its packages changed
280
- (`overrides`, patches, pnpm's `settings`, `catalogs`, or the version of
281
- pnpm itself), and a `package.json` change the lockfile doesn't record.
282
- Other lockfiles (`yarn.lock`, `bun.lockb`) are files like any other:
283
- outside the roots, a change to one runs everything.
284
-
285
- ```console
286
- $ bunx bivium plan
287
- bun.lock: @biomejs/biome 2.5.14 → 2.6.3, inert
288
- bun.lock: svelte 5.57.1 → 5.57.2
289
- unit tests: 4 of 12 files
290
- …
291
- ```
292
-
293
- ## Annotations
294
-
295
- | Comment | Meaning |
296
- |:---|:---|
297
- | `// @depends-on <glob> [glob…]` | Inputs that aren't imports: files a script reads, a tool it spawns, the pages a `goto()` the graph can't read opens (a loop over a list of URLs). Transitive. A file that reads the disk without one (or `@reads`, or `@lists`), or calls `goto()` with a URL it can't read, is unknown, unless it's `hermetic` |
298
- | `// @reads <glob> [glob…]` | Files read as text. Only a direct change to one counts, not a change to what it imports. The config's `reads` says the same for files that can't hold a comment |
299
- | `// @lists <glob> [glob…]` | Files whose names it lists, not their contents: a convention test that checks every route has a handler. Only adding, deleting or renaming one counts; an edit doesn't select it, and doesn't run its suite by the orphan rule either |
300
- | `// @glob-registry` | This file's `import.meta.glob` is a lookup table and adds no edges: whatever loads an entry names it itself |
301
-
302
- ## CLI
303
-
304
- | Command | Does |
305
- |:---|:---|
306
- | `bivium plan [base] [--json] [--github]` | Prints what it planned against (the base, its merge base, how many files changed, the config file), then each suite's selection (`none`, the files out of how many, `all` files when each reaches a change, or `all (full run)`) and why a suite runs in full. `--json` adds the base, merge base, config file, changed files, narrower results, changed packages, each suite's test count (`totals`), and the reasons as data (`causes`, each a `FullRunCause`: `{ kind, suites, file?, package?, narrower?, message }`). `--github` also writes each suite's selection to `$GITHUB_OUTPUT` |
307
- | `bivium run [base] [--suite <name>]` | Runs the suite's `run` command with its selection (default: the first suite), or skips it when nothing is affected |
308
- | `bivium why <test> [file] [--format tree\|chain\|source\|json] [--all]` | The shortest chain of edges from a changed file, or from `file`, to the test, with what spells each edge and the line it's on, and how many other changes reach the test. `--format tree` (the default) draws it from the test; `chain` reads from the change to the test; `source` prints each hop's line of source; `json` (or `--json`) prints the chain as data. `--all` shows every change's chain. For a test that isn't selected, it says what the file's chains reach instead |
309
- | `bivium check [--max-unknown <n>] [--json]` | Counts what it found by cause first, then lists every node with unknown dependencies, with where and how to fix each cause, and fails when more than `n` of them reach a test, or on one `knownUnknowns` doesn't list. A node no test depends on is listed apart and not counted: plans only run tests, so it widens nothing. Also lists the files under a suite's `root` that no test reaches, each with a fix (a change to one runs the whole suite; see [A change no test reaches](#a-change-no-test-reaches)), `tsconfig.json` files that don't parse, and [dead globs](#dead-globs), and fails on one `deadGlobs` doesn't allow or on an entry that matches none. Fails on a repository path a test spells that nothing declares (see [A change no test reaches](#a-change-no-test-reaches)). `--json` prints the report as data, with `ok` (what the exit status says) and `summary` (the counts the text leads with) |
310
- | `bivium graph [--suite <name>] [--top <n>] [--json]` | How many of each suite's tests a change to each file selects through the graph (leaving out the tests that run on any change, which it counts apart): the `n` files that reach the most, and how files spread from none to every test. A few hub files that reach most tests are what make plans wide. `--json` prints each suite's `total` and `everyChange` tests, and every node with its `imports` and `importers` (file edges), `fanIn` per suite and whether it's `unknown` |
311
-
312
- `--config`, `--cwd` and `--no-cache` work with every command,
313
- before or after it, and `why` and `check` take `--base <ref>`.
314
- An option takes its value as `--flag value` or `--flag=value`,
315
- and a switch such as `--json` also takes `=true` or `=false`.
316
- A path may be repo-relative, relative to the current directory
317
- (`./tests/a.test.ts`) or absolute.
318
- `--help` (or `-h`) prints usage.
319
- An unknown command, option, suite, test or `--format` is an error,
320
- which names what's close when something is
321
- (`unknown option --jsno (did you mean --json?)`).
322
- Exit status: 0 when done, 1 when `check` finds problems, 2 for a usage,
323
- configuration or git error; `run` exits with its command's status.
324
-
325
- Every JSON output carries `schemaVersion`, which is `1`;
326
- a change that drops or retypes a field bumps it, and new fields don't.
327
- With `--json`, an error is also printed to stdout as JSON,
328
- so a pipeline that parses stdout gets an object either way:
329
-
330
- ```json
331
- { "schemaVersion": 1, "error": { "kind": "git", "message": "bivium: no commit nosuchref here to diff against: fetch it." } }
332
- ```
333
-
334
- `kind` is `usage` (bivium was called wrong),
335
- `config`
336
- (the config can't be read or is invalid; the message names the file and
337
- lists every unknown key at once), `git`
338
- (the base or its history is missing, or git refused, in its own words)
339
- or `other`.
340
-
341
- ### From an agent
342
-
343
- - `bivium plan --json` gives the selection per suite and why:
344
- `causes` (typed) for each full run, `totals` for the scale, `changed`,
345
- `base` and `mergeBase`.
346
- - `bivium why <test> --format json` gives the chain from a change to a
347
- test, each link's `kind` (`import`, `type`, `goto`, `edges`…)
348
- and `at` (`file:line`).
349
- - `bivium check --json` gives `ok` and a `summary` of what widens plans,
350
- then each node with a `fix`.
351
- - Read the exit status first: 0 done, 1 `check` found problems,
352
- 2 an error, with `error.kind` in the JSON.
353
-
354
- `why` names each edge by what spells it:
355
- `import`, `type`
356
- (an `import type`, `export type … from`, JSDoc type, or an edge to or
357
- from a declaration file), `setup` (a suite's setup file),
358
- `goto` (a page `page.goto()` opens),
359
- `html`
360
- (a module an HTML page loads: `script[src]`, `link[href]`, `iframe[src]`
361
- or an inline script's import), `name`
362
- (a named import bound to a barrel name)
363
- and `export` (the file that exports it), `glob` (`import.meta.glob`),
364
- `depends-on`, `edges` (the config's), `reads` and `lists`.
20
+ **Every selection has a reason.**
21
+ `why` shows how a change reaches a test, file by file,
22
+ with the line that connects each one:
365
23
 
366
24
  ```console
367
25
  $ bunx bivium why e2e/accordion.test.ts
@@ -372,217 +30,64 @@ e2e/accordion.test.ts
372
30
  └─ name src/index.js#AccordionItem e2e/fixtures/Fixture.svelte:2
373
31
  └─ export src/AccordionItem.svelte src/index.js:2
374
32
  ● changed
375
- + 1 more change reaches this test: css/_accordion.scss (--all shows each chain)
376
-
377
- $ bunx bivium why e2e/accordion.test.ts --format chain
378
- src/AccordionItem.svelte changed
379
- exported as AccordionItem src/index.js:2
380
- imported by e2e/fixtures/Fixture.svelte:2
381
- imported by e2e/fixtures/accordion.ts:1
382
- loaded by page e2e/fixtures/accordion.html:10
383
- opened by e2e/accordion.test.ts:13 ← selected
384
- + 1 more change reaches this test: css/_accordion.scss (--all shows each chain)
385
-
386
- $ bunx bivium why e2e/accordion.test.ts --format source
387
- selected: e2e/accordion.test.ts ← src/AccordionItem.svelte (5 hops)
388
-
389
- --> e2e/accordion.test.ts:13 goto
390
- 13 | await page.goto("/accordion.html");
391
- --> e2e/fixtures/accordion.html:10 html
392
- 10 | <script type="module" src="./accordion.ts"></script>
393
- --> e2e/fixtures/accordion.ts:1 import
394
- 1 | import Fixture from "./Fixture.svelte";
395
- --> e2e/fixtures/Fixture.svelte:2 name
396
- 2 | import { Accordion, AccordionItem } from "carbon-components-svelte";
397
- --> src/index.js:2 export
398
- 2 | export { default as AccordionItem } from "./AccordionItem.svelte";
399
- ● src/AccordionItem.svelte: changed
400
- + 1 more change reaches this test: css/_accordion.scss (--all shows each chain)
401
- ```
402
-
403
- For a test that depends on every change, `chain` says why, where,
404
- and how to fix it, as `check` does:
405
-
406
- ```console
407
- $ bunx bivium why tests/e2e.test.ts --format chain
408
- tests/e2e.test.ts runs on every change
409
- because scripts/launcher.js:10 unresolved "./cli-entry.js"
410
- fix: no file at that path: fix the import, or add an `edges` entry if a build generates it
411
- reached via scripts/launcher.js
412
- depended on by scripts/build.ts:1
413
- depended on by tests/e2e.test.ts:2 ← selected
414
33
  ```
415
34
 
416
- ## Dead globs
417
-
418
- A typo'd `@depends-on`, or one pointing only at files the planner skips,
419
- looks fine and never selects its file.
420
- `check` evaluates every `@depends-on`, `@reads`,
421
- `@lists` and `import.meta.glob` pattern,
422
- every `edges` and `reads` value, and every suite's `setup`,
423
- against every file git lists
424
- (tracked and untracked, inside the roots or not) and reports one that:
425
-
426
- | Problem | Means |
427
- |:---|:---|
428
- | matches nothing | No file matches: a typo or a stale path |
429
- | matches only ignored files, partly ignored | Some matches are ignored and not in the graph, so a change to one selects nothing |
35
+ It has no dependencies and runs on Node ≥ 22 or Bun.
36
+ It started as
37
+ [carbon-components-svelte](https://github.com/carbon-design-system/carbon-components-svelte)'s
38
+ `test:changed`.
430
39
 
431
- A config value with captures is dead only when it matches nothing for
432
- every file its key matches, and a key that matches no file the graph
433
- reads is reported too, under the config's file.
434
- An ignored file the graph reads still seeds a walk,
435
- and a change outside the roots runs everything, so neither is dead.
436
- A glob that's dead on purpose goes in `deadGlobs` with a reason:
40
+ ## Quick start
437
41
 
438
- ```ts
439
- deadGlobs: [
440
- { file: "tests/css/tokens.test.ts", glob: "docs/src/**", reason: "docs-only changes skip CI" },
441
- ],
42
+ ```sh
43
+ bun add -d bivium
442
44
  ```
443
45
 
444
- ## CI
445
-
446
- Plan in a job of its own, before any install,
447
- and feed each suite's job its selection:
46
+ ```ts
47
+ // bivium.config.ts
48
+ import { defineConfig } from "bivium";
448
49
 
449
- ```yaml
450
- jobs:
451
- plan:
452
- runs-on: ubuntu-latest
453
- outputs:
454
- unit: ${{ steps.plan.outputs.unit-tests }}
455
- e2e: ${{ steps.plan.outputs.e2e-tests }}
456
- steps:
457
- - uses: actions/checkout@v7
458
- with:
459
- # History back to the merge base; see "The checkout" below.
460
- fetch-depth: 0
461
- filter: blob:none
462
- - uses: oven-sh/setup-bun@v2
463
- - id: merge-base
464
- run: echo "sha=$(git merge-base origin/main HEAD)" >> "$GITHUB_OUTPUT"
465
- - uses: actions/cache@v6
466
- with:
467
- path: .bivium/cache
468
- key: bivium-${{ steps.merge-base.outputs.sha }}
469
- restore-keys: bivium-
470
- - id: plan
471
- # --bun: run on Bun, which loads a TypeScript config whatever Node
472
- # the runner has.
473
- run: bunx --bun bivium plan origin/main --github
474
- unit:
475
- needs: plan
476
- if: needs.plan.outputs.unit != 'none'
477
- steps:
478
- # …install, then run all tests or the selected files:
479
- - run: bunx vitest run ${{ needs.plan.outputs.unit != 'all' && needs.plan.outputs.unit || '' }}
50
+ export default defineConfig({
51
+ suites: {
52
+ unit: { tests: "tests/**/*.test.ts", run: "vitest run {files}", root: "tests" },
53
+ },
54
+ roots: ["src", "tests"], // a changed file outside them runs everything
55
+ });
480
56
  ```
481
57
 
482
- ### The checkout
483
-
484
- A plan compares the branch with its merge base:
485
- the commit where it forked from the base branch.
486
- So the job needs the history back to that commit,
487
- not only the commit under test.
488
- `actions/checkout` fetches one commit by default (`fetch-depth: 1`),
489
- with no base branch, and the plan stops with what's missing:
490
-
491
- ```
492
- bivium: origin/main and HEAD share no commit here, so there's no merge base to diff against. The clone is shallow: fetch the history (actions/checkout: `fetch-depth: 0`).
58
+ ```sh
59
+ bunx bivium plan # which tests this branch needs
60
+ bunx bivium run # run them: vitest run tests/Button.test.ts …
493
61
  ```
494
62
 
495
- `fetch-depth: 0` fetches every commit.
496
- Only a job that plans needs it; one that runs every test anyway,
497
- such as a push to the default branch, can keep the default:
498
- `fetch-depth: ${{ github.event_name == 'pull_request' && '0' || '1' }}`.
499
-
500
- The history git fetches is three kinds of object:
501
- commits, trees
502
- (each directory's file names, each with the ID of its content)
503
- and blobs (the contents).
504
- A plan needs the commits, to find the merge base, and the trees,
505
- to list what changed.
506
- Of the blobs it needs the working tree's, which the checkout has,
507
- and the merge base's version of a few changed files only:
508
- a lockfile, a `package.json`, a `tsconfig.json`, a barrel,
509
- and what a narrower reads.
510
- So pick the clone by how big the history is:
511
-
512
- | History | Checkout | What a plan pays |
513
- | :--- | :--- | :--- |
514
- | Small: a full clone takes a second or two | `fetch-depth: 0` | Nothing more: every old version is already on disk |
515
- | Large: years of commits, big files | `fetch-depth: 0` and `filter: blob:none` | A fetch from the remote for the old versions it reads, when a change needs any: about a second, sometimes several |
516
-
517
- `filter: blob:none` makes a partial clone: every commit and tree,
518
- and only the blobs the checkout needs.
519
- Git fetches any other blob when something reads it,
520
- and fetches for reads made at once run in parallel.
521
- Without the filter, a large history's old contents are most of the
522
- download; with it, a small repository pays a round trip to the remote
523
- for contents it could have had for free.
524
-
525
- ### The cache
63
+ Exit status: 0 when done, 1 when `check` finds problems, 2 for an error;
64
+ `run` exits with its command's status.
526
65
 
527
- The cache holds what bivium extracted from each file, keyed by git blob,
528
- so a warm plan reads only the files a branch changed.
529
- Key it on the merge base: a pull request can restore caches from the
530
- default branch, whose run at that commit is the one to hit.
531
- Without `node_modules`, the cache lives in `.bivium/cache`;
532
- it writes a `.gitignore` so it never shows up as a change.
66
+ ## Common tasks
533
67
 
534
- ### What a plan costs
535
-
536
- Measured on an Apple M-series laptop under load
537
- (median of five, wall time):
68
+ ```sh
69
+ # Plan against a ref you name, as data
70
+ bivium plan origin/main --json
538
71
 
539
- | Repository | Files | Bun, no cache | Bun, warm | Node 24, no cache | Node 24, warm | Cache |
540
- |:---|---:|---:|---:|---:|---:|---:|
541
- | carbon-components-svelte | 4,680 | 0.15 s | 0.09 s | 0.27 s | 0.13 s | 452 KB |
542
- | generated TypeScript, 23 MB | 7,000 | 0.14 s | 0.08 s | 0.30 s | 0.12 s | 896 KB |
72
+ # Run one suite's selection
73
+ bivium run --suite e2e
543
74
 
544
- So the cache is optional at this size, `bunx --bun` is worth it,
545
- and planning inside a test job (no extra job, no install) costs well
546
- under a second.
547
- A cold plan of the generated repository peaks at 88 MB of RSS on Bun
548
- and 124 MB on Node.
75
+ # Why does this test run, or not?
76
+ bivium why tests/Button.test.ts
549
77
 
550
- ## Narrowers
78
+ # What bivium can't see, each with its fix
79
+ bivium check
551
80
 
552
- Some changes reach every test through a generated file:
553
- every page loads the compiled stylesheet,
554
- so any style edit selects every e2e test.
555
- A narrower replaces such a change's coarse seeds with finer ones from a
556
- semantic diff, such as the sources that render a changed CSS rule.
81
+ # Which files reach the most tests
82
+ bivium graph --top 10
557
83
 
558
- ```ts
559
- narrowers: [
560
- {
561
- name: "css",
562
- match: "styles/**", // the changed files it narrows
563
- opaque: "dist/*.css", // the generated nodes its seeds stand in for
564
- suites: ["e2e"],
565
- async narrow({ base, head, changed, graph }) {
566
- const rules = diffRules(await compile(base.commit), await compile(head.commit));
567
- if (!rules) return { all: "a rule that requires no class changed" };
568
- return { seeds: sourcesSpelling(rules), note: `${rules.length} rules changed` };
569
- },
570
- },
571
- ],
84
+ # In GitHub Actions: write each suite's selection to the step's outputs
85
+ bivium plan origin/main --github
572
86
  ```
573
87
 
574
- The matched files still seed the walk,
575
- but it stops at the `opaque` nodes, and the seeds stand in for them.
576
- A narrower can only narrow: its suites' selection is cut to what the
577
- walk without it selects.
578
- One that returns `{ all: reason }`, throws,
579
- or returns anything else runs its suites in full with the reason.
580
- `narrow()` gets the repository `root`,
581
- the merge base and the working tree as `base`
582
- and `head` (each with `commit` and `read(path)`),
583
- the matched `changed` files, and a read-only `graph`
584
- (`nodes()`, `importers(node)`, `dependents(nodes)`, `tests(suite)`).
585
- carbon-components-svelte runs one over crassus.
88
+ With `--json`, every command prints data with a `schemaVersion`,
89
+ and an error prints `{ "error": { "kind", "code", "message" } }` too.
90
+ These docs ship in the package, at `node_modules/bivium/docs/`.
586
91
 
587
92
  ## From code
588
93
 
@@ -594,21 +99,21 @@ const result = await plan({ config, base: "origin/main" });
594
99
  result.suites.unit; // { mode: "some", files: [...] }; result.suites.e2e is a type error
595
100
  ```
596
101
 
597
- `plan(options?)`
598
- takes `cwd`, `base`, `config` (default: the config file in `cwd`)
599
- and `cache`, and returns each suite's selection, its test count,
600
- the reasons as sentences and as data,
601
- the packages a lockfile change seeded or found inert, the base,
602
- merge base and changed files, and what each narrower returned.
603
- Given a config, the suite names carry through to the result's type.
604
- The package exports only `plan`, `defineConfig` and their types.
605
-
606
- ## Compared with
607
-
608
- | | Selects | Misses |
609
- |:---|:---|:---|
610
- | `vitest --changed`, `jest --changedSince` | Tests whose module graph reaches a changed file, from the runner's own resolver | Disk reads, fixtures, pages a test visits, config and setup wiring |
611
- | Playwright `--only-changed` | Test files whose imports changed | The pages a test opens and what they load |
612
- | Nx, Turborepo | Whole projects whose inputs changed | Nothing within a project's declared inputs, at project granularity |
613
- | Bazel | Targets whose declared inputs changed | Nothing, if every input is declared |
614
- | bivium | Test files, across runners and file types, from one graph | Nothing it can see; what it can't, it runs |
102
+ ## Docs
103
+
104
+ - [Config](https://github.com/metonym/bivium/blob/master/docs/config.md):
105
+ every key, and the `@depends-on`, `@reads` and `@lists` comments
106
+ - [Tuning a plan](https://github.com/metonym/bivium/blob/master/docs/tuning.md):
107
+ when a change runs too much, and what `check` reports
108
+ - [Recipes](https://github.com/metonym/bivium/blob/master/docs/recipes.md):
109
+ Vitest, Jest, Playwright, pnpm workspaces, generated code
110
+ - [CLI](https://github.com/metonym/bivium/blob/master/docs/cli.md):
111
+ every command and option, the JSON, and reading `why`
112
+ - [CI](https://github.com/metonym/bivium/blob/master/docs/ci.md):
113
+ GitHub Actions, the checkout a plan needs, the cache,
114
+ and what a plan costs
115
+ - [API](https://github.com/metonym/bivium/blob/master/docs/api.md):
116
+ `plan()`, and narrowers for generated files
117
+ - [How it works](https://github.com/metonym/bivium/blob/master/docs/how-it-works.md):
118
+ what the graph follows, lockfiles, and how bivium compares with
119
+ `vitest --changed`, Playwright, Nx and Bazel