@cosyte/vitest-config 0.1.3 → 0.1.4

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/stryker.js ADDED
@@ -0,0 +1,102 @@
1
+ // @cosyte/vitest-config/stryker
2
+ //
3
+ // THE SHARED STRYKERJS CONFIG, for mutation testing with the Vitest runner. It sits in this package
4
+ // because it runs the same Vitest suites over the same source files this package's coverage config
5
+ // measures, and every package that would use it already depends on this one. StrykerJS itself is an
6
+ // optional peer: a package that does not run mutation testing never installs it.
7
+
8
+ import { fileURLToPath } from "node:url";
9
+
10
+ /** The directory every run writes its reports under, relative to the directory Stryker runs in. */
11
+ export const REPORT_DIR = "reports/mutation";
12
+
13
+ /** The full run's JSON report: the mutation score the floor file holds is read from here. */
14
+ export const FULL_REPORT = `${REPORT_DIR}/mutation.json`;
15
+
16
+ /**
17
+ * The changed-files run's JSON report. It is a separate file so that a run over a few files can
18
+ * never be read as the whole package's score.
19
+ */
20
+ export const CHANGED_REPORT = `${REPORT_DIR}/changed.json`;
21
+
22
+ /**
23
+ * The files a run mutates by default: the source `@cosyte/vitest-config` measures coverage over,
24
+ * without the files it leaves out (tests, barrels, declarations, fixtures, generated code).
25
+ */
26
+ export const DEFAULT_MUTATE = Object.freeze([
27
+ "src/**/*.ts",
28
+ "!src/**/*.test.ts",
29
+ "!src/**/*.spec.ts",
30
+ "!src/**/*.d.ts",
31
+ "!src/**/index.ts",
32
+ "!src/**/__fixtures__/**",
33
+ "!src/**/generated/**",
34
+ ]);
35
+
36
+ /**
37
+ * The HTML report that sits beside a JSON report, for a person reading the surviving mutants.
38
+ *
39
+ * @param {string} jsonReport A report path ending in `.json`.
40
+ * @returns {string} The same path ending in `.html`.
41
+ */
42
+ export function htmlReportFor(jsonReport) {
43
+ return jsonReport.replace(/\.json$/, ".html");
44
+ }
45
+
46
+ /**
47
+ * The absolute path of the Vitest runner plugin. Stryker loads plugins named by a bare specifier
48
+ * from its own install, and under pnpm that install cannot see a sibling package, so a run would
49
+ * start with no test runner. A path resolved from this package works under pnpm, npm and Yarn.
50
+ *
51
+ * @param {(specifier: string) => string} [resolve] Resolves a specifier to a module URL.
52
+ * @returns {string} The plugin's entry file.
53
+ */
54
+ export function resolveVitestRunner(resolve = (specifier) => import.meta.resolve(specifier)) {
55
+ try {
56
+ return fileURLToPath(resolve("@stryker-mutator/vitest-runner"));
57
+ } catch (cause) {
58
+ throw new Error(
59
+ "@cosyte/vitest-config/stryker: @stryker-mutator/vitest-runner is not installed. Add it and " +
60
+ "@stryker-mutator/core as devDependencies.",
61
+ { cause },
62
+ );
63
+ }
64
+ }
65
+
66
+ /**
67
+ * The shared StrykerJS config for `@cosyte/*` packages: the Vitest runner with per-test coverage
68
+ * in Vitest's forks pool, static mutants kept, and the JSON report written to {@link FULL_REPORT}. A run never fails on
69
+ * its score (`break: null`): the floor file and `cosyte-ratchet` hold that rule, so there is one
70
+ * place that decides whether a score dropped.
71
+ *
72
+ * @param {object} [opts] Options.
73
+ * @param {readonly string[]} [opts.mutate] The files to mutate, in Stryker's glob form. Defaults to {@link DEFAULT_MUTATE}.
74
+ * @param {string} [opts.vitestConfig] The Vitest config file the runner loads.
75
+ * @param {Record<string, unknown>} [opts.stryker] Any other Stryker option, merged last.
76
+ * @returns {Record<string, unknown> & { mutate: string[], plugins: string[] }} The Stryker config.
77
+ * @example
78
+ * // stryker.config.mjs
79
+ * import { cosyteStryker } from "@cosyte/vitest-config/stryker";
80
+ * export default cosyteStryker();
81
+ */
82
+ export function cosyteStryker(opts = {}) {
83
+ const { mutate = DEFAULT_MUTATE, vitestConfig, stryker = {} } = opts;
84
+ return {
85
+ testRunner: "vitest",
86
+ plugins: [resolveVitestRunner()],
87
+ coverageAnalysis: "perTest",
88
+ mutate: [...mutate],
89
+ ignoreStatic: false,
90
+ reporters: ["clear-text", "progress-append-only", "html", "json"],
91
+ jsonReporter: { fileName: FULL_REPORT },
92
+ htmlReporter: { fileName: htmlReportFor(FULL_REPORT) },
93
+ thresholds: { high: 100, low: 90, break: null },
94
+ tempDirName: ".stryker-tmp",
95
+ ignorePatterns: ["coverage", "reports", "dist"],
96
+ // Vitest's own default pool, which Stryker's runner does not use unless told: under its
97
+ // default, worker threads, a test that calls process.chdir() fails before any mutant runs.
98
+ vitest:
99
+ vitestConfig === undefined ? { pool: "forks" } : { pool: "forks", configFile: vitestConfig },
100
+ ...stryker,
101
+ };
102
+ }