@cosyte/vitest-config 0.0.2 → 0.0.3
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/CHANGELOG.md +9 -4
- package/README.md +8 -8
- package/package.json +1 -1
- package/snippets.d.ts +1 -1
- package/snippets.js +12 -12
package/CHANGELOG.md
CHANGED
|
@@ -6,15 +6,20 @@ this file is hand-maintained. The package stays on the **`0.0.x`-until-first-alp
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- Documentation and source comments no longer use em dashes, in line with the cosyte brand
|
|
12
|
+
voice. No config or behaviour change.
|
|
13
|
+
|
|
9
14
|
### Added
|
|
10
15
|
|
|
11
|
-
- Declared `vite` (`^6 || ^7 || ^8`) as a peer dependency
|
|
16
|
+
- Declared `vite` (`^6 || ^7 || ^8`) as a peer dependency. Vitest 4 requires vite >= 6, and the
|
|
12
17
|
resolver otherwise keeps an incompatible vite 5 (missing the `./module-runner` export). _(Added after
|
|
13
18
|
`0.0.1` published; ships in the next release.)_
|
|
14
|
-
- **Doc/code-agreement harness** on a new `@cosyte/vitest-config/snippets` subpath
|
|
19
|
+
- **Doc/code-agreement harness** on a new `@cosyte/vitest-config/snippets` subpath: `docSnippetSuite()`
|
|
15
20
|
plus the factored primitives (`extractRunnableSnippets`, `rewriteAssertions`, `remapImports`,
|
|
16
21
|
`runSnippet`). It extracts every fenced ` ```ts runnable ` block from a package's `docs-content/`,
|
|
17
|
-
compiles it, executes it against the package, and asserts its inline `// => value` results
|
|
22
|
+
compiles it, executes it against the package, and asserts its inline `// => value` results: the
|
|
18
23
|
documentation analog of the conformance runners, so a green docs build can never carry a snippet
|
|
19
24
|
that silently disagrees with the code. devDep-only; Vitest is already a peer, so no new dependency.
|
|
20
25
|
|
|
@@ -22,5 +27,5 @@ this file is hand-maintained. The package stays on the **`0.0.x`-until-first-alp
|
|
|
22
27
|
|
|
23
28
|
### Added
|
|
24
29
|
|
|
25
|
-
- Initial release
|
|
30
|
+
- Initial release of `cosyteVitest(opts)`: v8 coverage, standard excludes, and enabled, gating
|
|
26
31
|
per-directory thresholds at >= 90.
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ declarations), and **enabled, gating** per-directory thresholds at **>= 90**.
|
|
|
10
10
|
pnpm add -D @cosyte/vitest-config vitest @vitest/coverage-v8 vite
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
`vitest`, `@vitest/coverage-v8`, and `vite` are peer dependencies (Vitest 4 needs `vite` >= 6
|
|
13
|
+
`vitest`, `@vitest/coverage-v8`, and `vite` are peer dependencies (Vitest 4 needs `vite` >= 6: install
|
|
14
14
|
it explicitly so the resolver doesn't keep an incompatible vite 5).
|
|
15
15
|
|
|
16
16
|
## Use
|
|
@@ -29,11 +29,11 @@ Each entry in `coverageDirs` adds a per-directory `src/<dir>/**` gate at >= 90 o
|
|
|
29
29
|
gate. Use `coverageThresholds` to add or override specific keys, and `test` for any other Vitest
|
|
30
30
|
options.
|
|
31
31
|
|
|
32
|
-
## Doc/code agreement
|
|
32
|
+
## Doc/code agreement: `@cosyte/vitest-config/snippets`
|
|
33
33
|
|
|
34
34
|
The `/snippets` subpath is the **documentation analog of the conformance runners**: it proves the
|
|
35
35
|
examples in a package's `docs-content/` still do what the prose claims. A copy-pasteable snippet that
|
|
36
|
-
silently returns the wrong field
|
|
36
|
+
silently returns the wrong field (a dose, a code, an identifier) is a clinical-harm failure mode
|
|
37
37
|
wearing a documentation costume, so a green docs build carrying a wrong snippet is exactly what this
|
|
38
38
|
prevents.
|
|
39
39
|
|
|
@@ -49,7 +49,7 @@ warnings.length; // => 0
|
|
|
49
49
|
````
|
|
50
50
|
|
|
51
51
|
`docSnippetSuite()` walks a docs directory, turns each runnable block into a Vitest `test` labelled by
|
|
52
|
-
file and line, compiles it, and executes it
|
|
52
|
+
file and line, compiles it, and executes it: a line of the form `<expr>; // => <value>` becomes
|
|
53
53
|
`expect(<expr>).toStrictEqual(<value>)`. A block tagged ` ```ts runnable throws ` must throw instead.
|
|
54
54
|
|
|
55
55
|
```ts
|
|
@@ -59,7 +59,7 @@ import { docSnippetSuite } from "@cosyte/vitest-config/snippets";
|
|
|
59
59
|
|
|
60
60
|
docSnippetSuite({
|
|
61
61
|
docsDir: join(import.meta.dirname, "..", "docs-content"),
|
|
62
|
-
// Point a snippet's `import ... from "@cosyte/hl7"` at what you want to prove against
|
|
62
|
+
// Point a snippet's `import ... from "@cosyte/hl7"` at what you want to prove against. This repo's
|
|
63
63
|
// source for a fast local gate, or `../dist/index.js` (after `pnpm build`) for artifact fidelity.
|
|
64
64
|
resolve: (spec) =>
|
|
65
65
|
spec === "@cosyte/hl7" ? join(import.meta.dirname, "..", "src", "index.ts") : undefined,
|
|
@@ -68,13 +68,13 @@ docSnippetSuite({
|
|
|
68
68
|
|
|
69
69
|
Options: `docsDir` / `files`, `include` (default `.md` / `.mdx`), `resolve` (import-specifier
|
|
70
70
|
remapper), `runnableTag` (default `"runnable"`), `name`, `requireSnippet`, `tmpDir`. A package with no
|
|
71
|
-
runnable snippets yields an empty, passing suite
|
|
71
|
+
runnable snippets yields an empty, passing suite: absence degrades quietly; a _wrong_ snippet fails
|
|
72
72
|
loudly. The primitives (`extractRunnableSnippets`, `rewriteAssertions`, `remapImports`, `runSnippet`)
|
|
73
73
|
are exported too, for bespoke wiring.
|
|
74
74
|
|
|
75
75
|
> `runSnippet` writes a transient `.ts` module under `tmpDir` (default `.cosyte-doc-snippets/` in the
|
|
76
|
-
> project root) and imports it so Vitest transforms the TypeScript
|
|
76
|
+
> project root) and imports it so Vitest transforms the TypeScript: add `.cosyte-doc-snippets*/` to
|
|
77
77
|
> `.gitignore`. It must stay inside the project root; files under `node_modules` are not transformed.
|
|
78
78
|
|
|
79
|
-
Part of [cosyte/config](https://github.com/cosyte/config)
|
|
79
|
+
Part of [cosyte/config](https://github.com/cosyte/config), one enforced toolchain for the `@cosyte/*`
|
|
80
80
|
suite.
|
package/package.json
CHANGED
package/snippets.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The doc/code-agreement harness
|
|
2
|
+
* The doc/code-agreement harness: the documentation analog of the parser conformance runners.
|
|
3
3
|
*
|
|
4
4
|
* Every fenced block a `docs-content` page opts in (```` ```ts runnable ````) is extracted, its
|
|
5
5
|
* inline `// => value` assertions compiled to real `expect(...)` checks, and the whole block executed
|
package/snippets.js
CHANGED
|
@@ -5,20 +5,20 @@ import { pathToFileURL } from "node:url";
|
|
|
5
5
|
import { afterAll, describe, expect, test } from "vitest";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* The doc/code-agreement harness
|
|
8
|
+
* The doc/code-agreement harness: the documentation analog of the parser conformance runners.
|
|
9
9
|
*
|
|
10
10
|
* A `docs-content` example a developer can copy into a production integration is only safe if it
|
|
11
11
|
* still does what the prose claims *against the built package*. A snippet that silently mis-reads a
|
|
12
12
|
* dose, an allergy, a code system, or a patient identifier is the clinical-harm failure mode wearing
|
|
13
|
-
* a documentation costume
|
|
13
|
+
* a documentation costume, so a green docs build carrying a wrong snippet is the exact thing this
|
|
14
14
|
* catches. Every fenced block a package opts in (```` ```ts runnable ````) is extracted, its inline
|
|
15
15
|
* `// => value` assertions are compiled to real `expect(...)` checks, and the whole block is executed
|
|
16
|
-
* as an ES module in the running Vitest process. If it throws
|
|
16
|
+
* as an ES module in the running Vitest process. If it throws, or an assertion fails, the docs
|
|
17
17
|
* build fails, loudly, next to the file and line.
|
|
18
18
|
*
|
|
19
19
|
* Lives in `@cosyte/vitest-config` (not the framework-agnostic `@cosyte/test-utils`) on purpose:
|
|
20
20
|
* {@link docSnippetSuite} registers Vitest `describe`/`test` blocks, so it belongs with the Vitest
|
|
21
|
-
* config every parser already depends on
|
|
21
|
+
* config every parser already depends on: zero new devDep, one versioned implementation on the
|
|
22
22
|
* Changesets train, devDep-only.
|
|
23
23
|
*
|
|
24
24
|
* @packageDocumentation
|
|
@@ -34,8 +34,8 @@ let moduleCounter = 0;
|
|
|
34
34
|
* Extract the opt-in runnable snippets from a markdown string.
|
|
35
35
|
*
|
|
36
36
|
* A block is runnable when its fence info string names a TypeScript language *and* carries the
|
|
37
|
-
* opt-in tag, e.g. ```` ```ts runnable ````. Everything else
|
|
38
|
-
*
|
|
37
|
+
* opt-in tag, e.g. ```` ```ts runnable ````. Everything else (prose, shell, un-tagged `ts` blocks)
|
|
38
|
+
* is ignored: opt-in, never opt-out, so documenting non-executable pseudocode stays possible.
|
|
39
39
|
* The `throws` meta tag marks a block expected to throw rather than run clean.
|
|
40
40
|
*
|
|
41
41
|
* @param {string} markdown - The markdown source.
|
|
@@ -95,7 +95,7 @@ export function extractRunnableSnippets(markdown, opts = {}) {
|
|
|
95
95
|
* Rewrite inline doctest assertions into real `expect(...)` calls.
|
|
96
96
|
*
|
|
97
97
|
* A line of the form `<expression>; // => <value>` becomes `expect(<expression>).toStrictEqual(<value>)`.
|
|
98
|
-
* The right-hand side is any JavaScript expression (a literal, `[]`, an object, another call)
|
|
98
|
+
* The right-hand side is any JavaScript expression (a literal, `[]`, an object, another call). It is
|
|
99
99
|
* evaluated in the snippet's own scope, so it can reference values the snippet defined. Lines without
|
|
100
100
|
* the `// =>` marker are left untouched and simply execute for their side effects (and must not throw).
|
|
101
101
|
*
|
|
@@ -142,7 +142,7 @@ export function remapImports(code, resolveFn) {
|
|
|
142
142
|
*
|
|
143
143
|
* The snippet is remapped, its assertions rewritten, an `expect` import prepended, and the result
|
|
144
144
|
* written to a unique temp `.ts` file under `tmpDir` (which MUST live inside the project root so
|
|
145
|
-
* Vitest transforms it
|
|
145
|
+
* Vitest transforms it: files under `node_modules` are treated as external and would reach Node as
|
|
146
146
|
* raw TypeScript). It is then dynamically imported: Vitest's module runner transforms the TypeScript
|
|
147
147
|
* and runs it. A block tagged `throws` must throw; any other block must run clean. The temp file is
|
|
148
148
|
* always removed afterwards.
|
|
@@ -170,7 +170,7 @@ export async function runSnippet(snippet, opts = {}) {
|
|
|
170
170
|
writeFileSync(file, moduleSource, "utf8");
|
|
171
171
|
try {
|
|
172
172
|
if (throwsExpected) {
|
|
173
|
-
// Weaker guarantee, by design: a `throws` block is green if the import rejects for ANY reason
|
|
173
|
+
// Weaker guarantee, by design: a `throws` block is green if the import rejects for ANY reason:
|
|
174
174
|
// the documented error, but also a compile error or a bad specifier. It proves "this does not
|
|
175
175
|
// run clean," not "this fails the specific way the prose claims." Assert the precise failure
|
|
176
176
|
// inside the snippet (a try/catch with an `// =>` on the caught code/message) when that matters.
|
|
@@ -198,7 +198,7 @@ export async function runSnippet(snippet, opts = {}) {
|
|
|
198
198
|
*
|
|
199
199
|
* Point it at a `docsDir` (or an explicit `files` list); it walks the markdown, turns each opt-in
|
|
200
200
|
* runnable block into a `test` labelled by file and line, and executes it against the built package.
|
|
201
|
-
* A package with no `docs-content` (or no runnable blocks) yields an empty, passing suite
|
|
201
|
+
* A package with no `docs-content` (or no runnable blocks) yields an empty, passing suite: the docs
|
|
202
202
|
* site's build-on-missing-content invariant has a testing analog here: absence degrades quietly, a
|
|
203
203
|
* *wrong* snippet fails loudly. Set `requireSnippet` when a repo wants to assert its docs carry at
|
|
204
204
|
* least one executed example.
|
|
@@ -255,7 +255,7 @@ export function docSnippetSuite(options = {}) {
|
|
|
255
255
|
});
|
|
256
256
|
} else if (total === 0) {
|
|
257
257
|
// A package with docs but no runnable examples yet (the common scaffold state) would otherwise
|
|
258
|
-
// leave this an empty suite
|
|
258
|
+
// leave this an empty suite, which Vitest reports as a "no test found in file" failure. A
|
|
259
259
|
// passing no-op keeps the wired suite green until the first snippet is tagged `runnable`.
|
|
260
260
|
test("no runnable snippets to check", () => {});
|
|
261
261
|
}
|
|
@@ -268,7 +268,7 @@ function defaultTmpDir() {
|
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
/**
|
|
271
|
-
* Recursively collect markdown files under a directory. A missing directory yields `[]
|
|
271
|
+
* Recursively collect markdown files under a directory. A missing directory yields `[]`: a package
|
|
272
272
|
* without `docs-content` produces an empty suite rather than a crash.
|
|
273
273
|
*
|
|
274
274
|
* @param {string} dir - Root directory.
|