bivium 0.6.0 → 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/docs/api.md ADDED
@@ -0,0 +1,69 @@
1
+ # API
2
+
3
+ `plan()` returns what `bivium plan --json` prints, typed by your config;
4
+ narrowers refine a plan from a semantic diff.
5
+
6
+ ## plan()
7
+
8
+ ```ts
9
+ import { defineConfig, plan } from "bivium";
10
+
11
+ const config = defineConfig({ suites: { unit: { tests: "tests/**/*.test.ts" } } });
12
+ const result = await plan({ config, base: "origin/main" });
13
+ result.suites.unit; // { mode: "some", files: [...] }; result.suites.e2e is a type error
14
+ ```
15
+
16
+ `plan(options?)`
17
+ takes `cwd`, `base`, `config` (default: the config file in `cwd`)
18
+ and `cache`, and returns each suite's selection, its test count,
19
+ the reasons as sentences and as data,
20
+ the packages a lockfile change seeded or found inert, the base,
21
+ merge base and changed files, and what each narrower returned.
22
+ Given a config, the suite names carry through to the result's type.
23
+ The package exports only `plan`, `defineConfig` and their types.
24
+
25
+ ## Narrowers
26
+
27
+ Some changes reach every test through a generated file:
28
+ every page loads the compiled stylesheet,
29
+ so any style edit selects every e2e test.
30
+ A narrower replaces such a change's coarse seeds with finer ones from a
31
+ semantic diff, such as the sources that render a changed CSS rule.
32
+
33
+ ```ts
34
+ narrowers: [
35
+ {
36
+ name: "css",
37
+ match: "styles/**", // the changed files it narrows
38
+ opaque: "dist/*.css", // the generated nodes its seeds stand in for
39
+ suites: ["e2e"],
40
+ async narrow({ base, head, changed, graph }) {
41
+ const rules = diffRules(await compile(base.commit), await compile(head.commit));
42
+ if (!rules) return { all: "a rule that requires no class changed" };
43
+ return { seeds: sourcesSpelling(rules), note: `${rules.length} rules changed` };
44
+ },
45
+ },
46
+ ],
47
+ ```
48
+
49
+ The matched files still seed the walk,
50
+ but it stops at the `opaque` nodes, and the seeds stand in for them.
51
+ A narrower can only narrow: its suites' selection is cut to what the
52
+ walk without it selects.
53
+ One that returns `{ all: reason }`, throws,
54
+ or returns anything else runs its suites in full with the reason.
55
+ `narrow()` gets:
56
+
57
+ - `root`, the repository;
58
+ - `base` and `head`, the merge base and the working tree,
59
+ each with `commit` and `read(path)`;
60
+ - `changed`, the matched files;
61
+ - `graph`, read-only: `nodes()`, `importers(node)`,
62
+ `dependents(nodes)` and `tests(suite)`.
63
+
64
+ carbon-components-svelte runs one over crassus.
65
+
66
+ ## Related
67
+
68
+ - [Config](config.md): `defineConfig` and every key
69
+ - [CLI](cli.md): the same plan from the command line
package/docs/ci.md ADDED
@@ -0,0 +1,112 @@
1
+ # CI
2
+
3
+ Plan in a job of its own, before any install,
4
+ and skip the test jobs whose selection is `none`.
5
+
6
+ Plan in a job of its own, before any install,
7
+ and feed each suite's job its selection:
8
+
9
+ ```yaml
10
+ jobs:
11
+ plan:
12
+ runs-on: ubuntu-latest
13
+ outputs:
14
+ unit: ${{ steps.plan.outputs.unit-tests }}
15
+ e2e: ${{ steps.plan.outputs.e2e-tests }}
16
+ steps:
17
+ - uses: actions/checkout@v7
18
+ with:
19
+ # History back to the merge base; see "The checkout" below.
20
+ fetch-depth: 0
21
+ filter: blob:none
22
+ - uses: oven-sh/setup-bun@v2
23
+ - id: merge-base
24
+ run: echo "sha=$(git merge-base origin/main HEAD)" >> "$GITHUB_OUTPUT"
25
+ - uses: actions/cache@v6
26
+ with:
27
+ path: .bivium/cache
28
+ key: bivium-${{ steps.merge-base.outputs.sha }}
29
+ restore-keys: bivium-
30
+ - id: plan
31
+ # --bun: run on Bun, which loads a TypeScript config whatever Node
32
+ # the runner has.
33
+ run: bunx --bun bivium plan origin/main --github
34
+ unit:
35
+ needs: plan
36
+ if: needs.plan.outputs.unit != 'none'
37
+ steps:
38
+ # …install, then run all tests or the selected files:
39
+ - run: bunx vitest run ${{ needs.plan.outputs.unit != 'all' && needs.plan.outputs.unit || '' }}
40
+ ```
41
+
42
+ ## The checkout
43
+
44
+ A plan compares the branch with its merge base:
45
+ the commit where it forked from the base branch.
46
+ So the job needs the history back to that commit,
47
+ not only the commit under test.
48
+ `actions/checkout` fetches one commit by default (`fetch-depth: 1`),
49
+ with no base branch, and the plan stops with what's missing:
50
+
51
+ ```text
52
+ 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`).
53
+ ```
54
+
55
+ `fetch-depth: 0` fetches every commit.
56
+ Only a job that plans needs it; one that runs every test anyway,
57
+ such as a push to the default branch, can keep the default:
58
+ `fetch-depth: ${{ github.event_name == 'pull_request' && '0' || '1' }}`.
59
+
60
+ The history git fetches is three kinds of object: commits;
61
+ trees, each directory's file names with the ID of each one's content;
62
+ and blobs, the contents.
63
+ A plan needs the commits, to find the merge base, and the trees,
64
+ to list what changed.
65
+ Of the blobs it needs the working tree's, which the checkout has,
66
+ and the merge base's version of a few changed files only:
67
+ a lockfile, a `package.json`, a `tsconfig.json`, a barrel,
68
+ and what a narrower reads.
69
+ So pick the clone by how big the history is:
70
+
71
+ | History | Checkout | What a plan pays |
72
+ | :--- | :--- | :--- |
73
+ | Small: a full clone takes a second or two | `fetch-depth: 0` | Nothing more: every old version is already on disk |
74
+ | 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 |
75
+
76
+ `filter: blob:none` makes a partial clone: every commit and tree,
77
+ and only the blobs the checkout needs.
78
+ Git fetches any other blob when something reads it,
79
+ and fetches for reads made at once run in parallel.
80
+ Without the filter, a large history's old contents are most of the
81
+ download; with it, a small repository pays a round trip to the remote
82
+ for contents it could have had for free.
83
+
84
+ ## The cache
85
+
86
+ The cache holds what bivium extracted from each file, keyed by git blob,
87
+ so a warm plan reads only the files a branch changed.
88
+ Key it on the merge base: a pull request can restore caches from the
89
+ default branch, whose run at that commit is the one to hit.
90
+ Without `node_modules`, the cache lives in `.bivium/cache`;
91
+ it writes a `.gitignore` so it never shows up as a change.
92
+
93
+ ## What a plan costs
94
+
95
+ Measured on an Apple M-series laptop under load
96
+ (median of five, wall time):
97
+
98
+ | Repository | Files | Bun, no cache | Bun, warm | Node 24, no cache | Node 24, warm | Cache |
99
+ |:---|---:|---:|---:|---:|---:|---:|
100
+ | carbon-components-svelte | 4,680 | 0.15 s | 0.09 s | 0.27 s | 0.13 s | 452 KB |
101
+ | generated TypeScript, 23 MB | 7,000 | 0.14 s | 0.08 s | 0.30 s | 0.12 s | 896 KB |
102
+
103
+ So the cache is optional at this size, `bunx --bun` is worth it,
104
+ and planning inside a test job (no extra job, no install) costs well
105
+ under a second.
106
+ A cold plan of the generated repository peaks at 88 MB of RSS on Bun
107
+ and 124 MB on Node.
108
+
109
+ ## Related
110
+
111
+ - [CLI](cli.md): `plan --github` and the other commands
112
+ - [How it works](how-it-works.md): what a plan reads
package/docs/cli.md ADDED
@@ -0,0 +1,133 @@
1
+ # CLI
2
+
3
+ Five commands: `plan` picks the tests, `run` runs them,
4
+ `why` explains one, `check` finds what widens plans,
5
+ and `graph` shows where they fan out.
6
+
7
+ | Command | Does |
8
+ |:---|:---|
9
+ | `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` |
10
+ | `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 |
11
+ | `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 |
12
+ | `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](tuning.md#a-change-no-test-reaches)), `tsconfig.json` files that don't parse, and [dead globs](tuning.md#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](tuning.md#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) |
13
+ | `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` |
14
+
15
+ `--config`, `--cwd` and `--no-cache` work with every command,
16
+ before or after it, and `why` and `check` take `--base <ref>`.
17
+ An option takes its value as `--flag value` or `--flag=value`,
18
+ and a switch such as `--json` also takes `=true` or `=false`.
19
+ A path may be repo-relative, relative to the current directory
20
+ (`./tests/a.test.ts`) or absolute.
21
+ `--help` (or `-h`) prints usage.
22
+ An unknown command, option, suite, test or `--format` is an error,
23
+ which names what's close when something is
24
+ (`unknown option --jsno (did you mean --json?)`).
25
+ Exit status: 0 when done, 1 when `check` finds problems, 2 for a usage,
26
+ configuration or git error; `run` exits with its command's status.
27
+
28
+ Every JSON output carries `schemaVersion`, which is `1`;
29
+ a change that drops or retypes a field bumps it, and new fields don't.
30
+ With `--json`, an error is also printed to stdout as JSON,
31
+ so a pipeline that parses stdout gets an object either way:
32
+
33
+ ```json
34
+ { "schemaVersion": 1, "error": { "kind": "git", "code": "BV_GIT_NO_BASE", "message": "bivium: no commit nosuchref here to diff against: fetch it." } }
35
+ ```
36
+
37
+ | `kind` | Means |
38
+ | --- | --- |
39
+ | `usage` | bivium was called wrong |
40
+ | `config` | the config can't be read or is invalid; the message names the file and lists every unknown key at once |
41
+ | `git` | the base or its history is missing, or git refused, in its own words |
42
+ | `other` | a failure bivium didn't name |
43
+
44
+ `code` names the failure, such as `BV_UNKNOWN_OPTION`
45
+ or `BV_CONFIG_INVALID`, and never changes meaning:
46
+ the `BiviumErrorCode` type lists them all, each with what it means.
47
+ From the library, `plan()` throws a `BiviumError` with the same `code`
48
+ and `kind`.
49
+
50
+ ## From an agent
51
+
52
+ - `bivium plan --json` gives the selection per suite and why:
53
+ `causes` (typed) for each full run, `totals` for the scale, `changed`,
54
+ `base` and `mergeBase`.
55
+ - `bivium why <test> --format json` gives the chain from a change to a
56
+ test, with each link's `kind`, such as `import` or `goto`, and `at`,
57
+ as `file:line`.
58
+ - `bivium check --json` gives `ok` and a `summary` of what widens plans,
59
+ then each node with a `fix`.
60
+ - Read the exit status first: 0 done, 1 `check` found problems,
61
+ 2 an error, with `error.kind` and `error.code` in the JSON.
62
+
63
+ ## Reading `why`
64
+
65
+ `why` names each edge by what spells it:
66
+
67
+ | Edge | Spelled by |
68
+ | --- | --- |
69
+ | `import` | any other import: static, dynamic, `require()` or `vi.mock()` |
70
+ | `type` | an `import type`, `export type … from`, a JSDoc type, or an edge to or from a declaration file |
71
+ | `setup` | a suite's setup file |
72
+ | `goto` | a page `page.goto()` opens |
73
+ | `html` | a module an HTML page loads: `script[src]`, `link[href]`, `iframe[src]` or an inline script's import |
74
+ | `name`, `export` | a named import bound to a barrel name, and the file that exports it |
75
+ | `glob` | `import.meta.glob` |
76
+ | `depends-on`, `reads`, `lists` | the annotations |
77
+ | `edges` | the config's `edges` |
78
+
79
+ ```console
80
+ $ bunx bivium why e2e/accordion.test.ts
81
+ e2e/accordion.test.ts
82
+ └─ goto e2e/fixtures/accordion.html e2e/accordion.test.ts:13
83
+ └─ html e2e/fixtures/accordion.ts e2e/fixtures/accordion.html:10
84
+ └─ import e2e/fixtures/Fixture.svelte e2e/fixtures/accordion.ts:1
85
+ └─ name src/index.js#AccordionItem e2e/fixtures/Fixture.svelte:2
86
+ └─ export src/AccordionItem.svelte src/index.js:2
87
+ ● changed
88
+ + 1 more change reaches this test: css/_accordion.scss (--all shows each chain)
89
+
90
+ $ bunx bivium why e2e/accordion.test.ts --format chain
91
+ src/AccordionItem.svelte changed
92
+ exported as AccordionItem src/index.js:2
93
+ imported by e2e/fixtures/Fixture.svelte:2
94
+ imported by e2e/fixtures/accordion.ts:1
95
+ loaded by page e2e/fixtures/accordion.html:10
96
+ opened by e2e/accordion.test.ts:13 ← selected
97
+ + 1 more change reaches this test: css/_accordion.scss (--all shows each chain)
98
+
99
+ $ bunx bivium why e2e/accordion.test.ts --format source
100
+ selected: e2e/accordion.test.ts ← src/AccordionItem.svelte (5 hops)
101
+
102
+ --> e2e/accordion.test.ts:13 goto
103
+ 13 | await page.goto("/accordion.html");
104
+ --> e2e/fixtures/accordion.html:10 html
105
+ 10 | <script type="module" src="./accordion.ts"></script>
106
+ --> e2e/fixtures/accordion.ts:1 import
107
+ 1 | import Fixture from "./Fixture.svelte";
108
+ --> e2e/fixtures/Fixture.svelte:2 name
109
+ 2 | import { Accordion, AccordionItem } from "carbon-components-svelte";
110
+ --> src/index.js:2 export
111
+ 2 | export { default as AccordionItem } from "./AccordionItem.svelte";
112
+ ● src/AccordionItem.svelte: changed
113
+ + 1 more change reaches this test: css/_accordion.scss (--all shows each chain)
114
+ ```
115
+
116
+ For a test that depends on every change, `chain` says why, where,
117
+ and how to fix it, as `check` does:
118
+
119
+ ```console
120
+ $ bunx bivium why tests/e2e.test.ts --format chain
121
+ tests/e2e.test.ts runs on every change
122
+ because scripts/launcher.js:10 unresolved "./cli-entry.js"
123
+ fix: no file at that path: fix the import, or add an `edges` entry if a build generates it
124
+ reached via scripts/launcher.js
125
+ depended on by scripts/build.ts:1
126
+ depended on by tests/e2e.test.ts:2 ← selected
127
+ ```
128
+
129
+ ## Related
130
+
131
+ - [CI](ci.md): planning in GitHub Actions
132
+ - [Tuning a plan](tuning.md): fixing what `check` reports
133
+ - [API](api.md): the same plan from code
package/docs/config.md ADDED
@@ -0,0 +1,59 @@
1
+ # Config
2
+
3
+ bivium reads `bivium.config.ts` from the repository root;
4
+ every key, and the comments a file can carry, are below.
5
+
6
+ `bivium.config.ts` (or `.mts`, `.mjs`, `.js`, `.json`) in the repository
7
+ root, or `--config <file>`.
8
+ A `.ts` config needs Bun or Node ≥ 22.18.
9
+ Every glob key takes one glob or a list.
10
+ An unknown key is an error, since a misspelled one would quietly select
11
+ less.
12
+
13
+ | Key | Meaning |
14
+ |:---|:---|
15
+ | `suites.<name>.tests` | The suite's test files |
16
+ | `suites.<name>.run` | The command `bivium run` runs. `{files}` is replaced by the selection (nothing for `all`); without it, the files are appended |
17
+ | `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 |
18
+ | `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 |
19
+ | `suites.<name>.pages` | Pages only a `goto()` opens; files only they load are wired to no test |
20
+ | `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 |
21
+ | `suites.<name>.wiring` | Files whose change runs the whole suite: another harness, a runner config |
22
+ | `suites.<name>.nonInputs` | Files under `root` the runner never reads, such as type-check inputs |
23
+ | `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 |
24
+ | `suites.<name>.output` | The step output `plan --github` writes. Default: `<name>-tests` |
25
+ | `roots` | Paths (not globs) the graph reads. A changed file outside them that isn't ignored runs everything. Default: the whole repository |
26
+ | `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 |
27
+ | `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` |
28
+ | `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/**" }` |
29
+ | `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" }` |
30
+ | `self` | The selector's own files: a change to one runs everything. The config file is always one |
31
+ | `importOnly` | Files only an import reaches: deleting one never orphans a suite |
32
+ | `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 |
33
+ | `barrels` | Modules that may bind named imports. Default: every module made only of re-exports |
34
+ | `external` | Packages a dependency installs that your code imports without declaring |
35
+ | `inert` | Packages no test uses except by importing them: linters, formatters, type packages (`["@biomejs/biome", "knip", "@types/*"]`). A [lockfile change](how-it-works.md#lockfiles) that only they reach selects nothing. With `loadedByName` set, only packages with a `bin` need listing. bivium itself always is |
36
+ | `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](how-it-works.md#lockfiles) that reaches one no test imports selects only its importers. A package with a `bin` always counts as one a test may run |
37
+ | `extensions` | Suffixes tried after a specifier |
38
+ | `deadGlobs` | Dead globs `check` allows, each `{ file, glob, reason }`, the glob as `file` spells it |
39
+ | `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 |
40
+ | `narrowers` | See [Narrowers](api.md#narrowers) |
41
+ | `base` | The default base ref. Otherwise the upstream default branch (`origin/HEAD`, then `origin/main`, `origin/master`, `main`, `master`) |
42
+ | `cache` | The cache's directory, or `false`. Default: `node_modules/.cache/bivium`, or `.bivium/cache` |
43
+
44
+ ## Annotations
45
+
46
+ | Comment | Meaning |
47
+ |:---|:---|
48
+ | `// @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` |
49
+ | `// @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 |
50
+ | `// @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 |
51
+ | `// @glob-registry` | This file's `import.meta.glob` is a lookup table and adds no edges: whatever loads an entry names it itself |
52
+
53
+ ## Related
54
+
55
+ - [Tuning a plan](tuning.md): which key to reach for
56
+ when a change runs too much
57
+ - [Recipes](recipes.md): Vitest, Jest, Playwright, pnpm workspaces,
58
+ generated code
59
+ - [How it works](how-it-works.md): what the graph follows
@@ -0,0 +1,106 @@
1
+ # How it works
2
+
3
+ bivium builds a static, file-level dependency graph
4
+ and walks it backwards from the files a change touches to the tests that
5
+ reach them; what it can't read counts as depending on everything.
6
+
7
+ ## What it reads
8
+
9
+ A one-pass lexer reads JavaScript and TypeScript
10
+ (`.js`, `.ts`, `.jsx`, `.tsx`, and their `m`/`c` variants),
11
+ the scripts and markup expressions of `.svelte`, the scripts of `.vue`,
12
+ `.astro` and `.html`, and the `script[src]`,
13
+ `link[href]` and `iframe[src]` of `.html`.
14
+ Only code counts: an import spelled in a string
15
+ or a comment adds no edge.
16
+
17
+ | It follows | As |
18
+ |:---|:---|
19
+ | 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 |
20
+ | `vi.mock()`, `vi.importActual()` and the rest of the family | Edges |
21
+ | `import.meta.glob()` | Every matching file, now and later |
22
+ | JSDoc `@type {import("x")}` and `@import` | Edges (TypeScript reads them) |
23
+ | `%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 |
24
+ | `page.goto("/x.html")` in a suite with `urls` | An edge to the page |
25
+ | `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 |
26
+ | `#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 |
27
+ | 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` |
28
+ | A file's own package and its ancestors' `package.json` dependencies, and builtins | `pkg:` nodes |
29
+ | Anything else | Unknown: the file depends on every change |
30
+
31
+ A root `bun.lock`, `package-lock.json` or `pnpm-lock.yaml` isn't a
32
+ source file: a change to one seeds the packages it changed
33
+ (see [Lockfiles](#lockfiles)).
34
+
35
+ Nor is a `tsconfig.json` (or a file one `extends`)
36
+ or a `package.json` under `roots`: their options, `paths`,
37
+ `type` or `exports` change how the files under them resolve or run,
38
+ which no import says.
39
+ A change to one seeds the files it governs,
40
+ those it's the nearest of its kind to.
41
+ A change the parsed file doesn't show, such as formatting or comments,
42
+ seeds nothing; so does one to a `package.json`'s metadata alone
43
+ (`version`, `description`, `repository` and the like),
44
+ and a dependency change is the lockfile's.
45
+
46
+ A file it can't lex cleanly is read by a regex extractor instead,
47
+ which over-approximates.
48
+ CSS isn't scanned: generated stylesheets reach their inputs through
49
+ `edges`, and a [narrower](api.md#narrowers) can do better.
50
+
51
+ ## Lockfiles
52
+
53
+ A dependency bump changes a lockfile, which no file imports.
54
+ When a root `bun.lock`, `package-lock.json` (v2 or v3),
55
+ or `pnpm-lock.yaml` v6 or v9 changes,
56
+ bivium diffs it against the merge base and seeds the walk with the
57
+ `pkg:` nodes of the packages whose entries changed,
58
+ and of every package that depends on one,
59
+ so a `svelte` bump selects what imports `svelte`,
60
+ or a package built on it.
61
+ A `package.json` whose only change is to dependencies the lockfile
62
+ records the same way counts as those packages changing.
63
+
64
+ A changed package can also be used in ways the graph can't see:
65
+ a CLI a test spawns, a runner, a type package.
66
+ So when a change reaches a package that a `package.json` declares,
67
+ or that some file imports, and that no test imports, everything runs,
68
+ with the reason
69
+ (`bun.lock: playwright 1.63.0 → 1.64.0, and no test imports it`),
70
+ unless the package is `inert`.
71
+ bivium itself always is: a bump changes which tests later plans pick,
72
+ not what a test does.
73
+ With `loadedByName` set, only a package with a `bin` in the lockfile,
74
+ or one it lists, counts as used that way:
75
+ a bump that reaches a library only a CMS config imports selects nothing.
76
+ Everything also runs for a lockfile bivium can't parse,
77
+ one whose settings other than its packages changed
78
+ (`overrides`, patches, pnpm's `settings`, `catalogs`, or the version of
79
+ pnpm itself), and a `package.json` change the lockfile doesn't record.
80
+ Other lockfiles (`yarn.lock`, `bun.lockb`) are files like any other:
81
+ outside the roots, a change to one runs everything.
82
+
83
+ ```console
84
+ $ bunx bivium plan
85
+ bun.lock: @biomejs/biome 2.5.14 → 2.6.3, inert
86
+ bun.lock: svelte 5.57.1 → 5.57.2
87
+ unit tests: 4 of 12 files
88
+ …
89
+ ```
90
+
91
+ ## Compared with
92
+
93
+ Where bivium sits among the tools that select or skip tests:
94
+
95
+ | | Selects | Misses |
96
+ |:---|:---|:---|
97
+ | `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 |
98
+ | Playwright `--only-changed` | Test files whose imports changed | The pages a test opens and what they load |
99
+ | Nx, Turborepo | Whole projects whose inputs changed | Nothing within a project's declared inputs, at project granularity |
100
+ | Bazel | Targets whose declared inputs changed | Nothing, if every input is declared |
101
+ | bivium | Test files, across runners and file types, from one graph | Nothing it can see; what it can't, it runs |
102
+
103
+ ## Related
104
+
105
+ - [Tuning a plan](tuning.md): the orphan rule and `check`
106
+ - [Config](config.md): telling it what it can't see
@@ -0,0 +1,67 @@
1
+ # Recipes
2
+
3
+ Settings for common test runners and repository shapes,
4
+ each a starting point to adjust.
5
+
6
+ **Vitest or Jest.**
7
+ Name the setup files the runner loads,
8
+ mirror `resolve.alias` (a Vite key like `"@/"` works as written),
9
+ give snapshots to their tests, and, for a suite that runs code,
10
+ skip type-only edges:
11
+
12
+ ```ts
13
+ suites: {
14
+ unit: {
15
+ tests: "src/**/*.test.{ts,tsx}",
16
+ run: "vitest run {files}",
17
+ root: "src",
18
+ setup: ["src/setupTests.ts"], // vitest.config.ts `setupFiles`
19
+ wiring: ["vitest.config.ts"],
20
+ types: false,
21
+ nonInputs: ["src/**/types.ts"], // types only: nothing loads them at runtime
22
+ },
23
+ },
24
+ alias: { "@/": "src/" },
25
+ reads: { "src/**/*.test.{ts,tsx}": "src/$1__snapshots__/$2.test.$3.snap" },
26
+ ```
27
+
28
+ tsconfig `paths` (what `vite-tsconfig-paths` reads) are read without an
29
+ `alias`.
30
+ An extensionless `vi.mock("@/x")` resolves as an import does.
31
+ A test environment the runner loads by name (`jsdom`, `happy-dom`) goes
32
+ in `loadedByName` if you set it.
33
+
34
+ **pnpm workspaces.**
35
+ Workspace packages resolve through their `package.json` `exports`,
36
+ and a `.js` specifier into one (`@acme/ui/Button.js`) reaches its `.ts`
37
+ source.
38
+ A `pnpm-lock.yaml` change selects through the packages it changed.
39
+ Set `roots` to the packages and apps whose changes the suites need to
40
+ see, and a suite per test runner.
41
+
42
+ **Playwright.**
43
+ Map `page.goto()` URLs to the pages they open with `urls`,
44
+ list pages only a `goto()` opens in `pages`,
45
+ and name `playwright.config.ts` in `wiring`:
46
+
47
+ ```ts
48
+ e2e: {
49
+ tests: "e2e/**/*.spec.ts",
50
+ root: "e2e",
51
+ urls: { "/*": "e2e/fixtures/*.html" },
52
+ wiring: ["playwright.config.ts"],
53
+ },
54
+ ```
55
+
56
+ **Generated code.**
57
+ A content or type generator that writes into a gitignored directory
58
+ (`.velite`, `.contentlayer/generated`) is an `edges` entry from what it
59
+ writes to what it reads:
60
+ `{ ".velite/*": ["content/**", "velite.config.ts"] }`.
61
+ An import of `#velite` or `../.velite` then reaches the content,
62
+ and not every change.
63
+
64
+ ## Related
65
+
66
+ - [Config](config.md): every key
67
+ - [Tuning a plan](tuning.md): when a plan still runs too much
package/docs/tuning.md ADDED
@@ -0,0 +1,91 @@
1
+ # Tuning a plan
2
+
3
+ A plan that runs more than a change needs has a cause bivium can name;
4
+ `bivium check` lists each one with its fix.
5
+
6
+ ## A change no test reaches
7
+
8
+ A changed file under a suite's `root` that no test of the suite reaches
9
+ is wired some other way (a runner config, a setup file),
10
+ so the whole suite runs: the orphan rule.
11
+ With the default `root`, the whole repository,
12
+ any file bivium can't trace to a test runs the suite,
13
+ which is sound and coarse.
14
+ Set `root` on every suite to the directories its tests
15
+ and sources live in, and say what the rest is:
16
+
17
+ | You changed… | and want… | Use |
18
+ |:---|:---|:---|
19
+ | a file no test can observe: docs, stories, a changelog | nothing to run, unless some test imports it | `ignore` |
20
+ | a file under the suite's `root` only another tool reads: type-check inputs | nothing to run for this suite | the suite's `nonInputs` |
21
+ | a runner config or a second harness | the whole suite | the suite's `wiring` |
22
+ | a setup file the runner loads before every test, or what it imports | every test of the suite | the suite's `setup` |
23
+ | the selector's config, or its own code | everything | `self` (the config file always is) |
24
+ | a declaration file, in a suite that runs code | nothing | the suite's `types: false` |
25
+ | 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` |
26
+ | 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 |
27
+ | a file a build turns into what tests load | the tests that load the output | `edges` from the output to its inputs |
28
+
29
+ A file outside `roots` that isn't ignored runs everything:
30
+ it's the backstop for wiring the graph can't see, such as a CI workflow.
31
+
32
+ `bivium check` lists every file under a suite's `root` that no test
33
+ reaches and the config doesn't exempt,
34
+ with a fix for each by what it looks like: a test's snapshot,
35
+ a runner's config, or anything else.
36
+ It's the list to work through when adopting bivium,
37
+ and a new entry is a gap someone added.
38
+
39
+ A file's own disk reads make it unknown,
40
+ but a test that hands a path to the code under test
41
+ (`typecheck({ workspace: "tests/fixtures" })`),
42
+ or a `hermetic` one that reads it, reads nothing the graph can see.
43
+ So `check` also reads the string literals in each test,
44
+ and in what the tests reach under a suite's `root`,
45
+ for repository paths: relative to the root when they have a slash,
46
+ as `join(root, "tests/fixtures")` spells one, and relative to the file,
47
+ as `new URL("fixtures", import.meta.url)` does.
48
+ It fails on one a change to which wouldn't select the tests that reach
49
+ the file, with the `reads`, `edges` or annotation that declares it.
50
+ A path whose change runs the suite anyway, as `wiring`,
51
+ `self` or by the orphan rule, doesn't fail,
52
+ and an unreached file's fix names where it's spelled.
53
+ Strings inside brackets are skipped as data
54
+ (`["src/a.ts", …]`, `map["key"]`), as are the directories a file is in
55
+ and paths outside the graph.
56
+ A path a test only names, such as an expected output,
57
+ takes `// @lists <path>`.
58
+
59
+ ## Dead globs
60
+
61
+ A typo'd `@depends-on`, or one pointing only at files the planner skips,
62
+ looks fine and never selects its file.
63
+ `check` evaluates every `@depends-on`, `@reads`,
64
+ `@lists` and `import.meta.glob` pattern,
65
+ every `edges` and `reads` value, and every suite's `setup`,
66
+ against every file git lists
67
+ (tracked and untracked, inside the roots or not) and reports one that:
68
+
69
+ | Problem | Means |
70
+ |:---|:---|
71
+ | matches nothing | No file matches: a typo or a stale path |
72
+ | matches only ignored files, partly ignored | Some matches are ignored and not in the graph, so a change to one selects nothing |
73
+
74
+ A config value with captures is dead only when it matches nothing for
75
+ every file its key matches, and a key that matches no file the graph
76
+ reads is reported too, under the config's file.
77
+ An ignored file the graph reads still seeds a walk,
78
+ and a change outside the roots runs everything, so neither is dead.
79
+ A glob that's dead on purpose goes in `deadGlobs` with a reason:
80
+
81
+ ```ts
82
+ deadGlobs: [
83
+ { file: "tests/css/tokens.test.ts", glob: "docs/src/**", reason: "docs-only changes skip CI" },
84
+ ],
85
+ ```
86
+
87
+ ## Related
88
+
89
+ - [Config](config.md): every key named here
90
+ - [CLI](cli.md): `check`, `why` and `graph`
91
+ - [Recipes](recipes.md): settings for common runners
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bivium",
3
- "version": "0.6.0",
4
- "description": "Run only the tests a change can affect, and never miss one.",
3
+ "version": "0.6.1",
4
+ "description": "Run only the tests your change can reach.",
5
5
  "license": "MIT",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
7
7
  "repository": {