Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

vitest.mutants.config.ts

44 lines1.5 KB

1import { appendFileSync } from 'node:fs'
2import { resolve } from 'node:path'
3import { mergeConfig } from 'vitest/config'
4import baseConfig from './vite.config'
5import type { Plugin } from 'vite'
6​
7/**
8 * Applies one mutant from `scripts/demand-coverage-mutants.mjs` while vitest
9 * transforms the module, so no file on disk changes and other test runs in
10 * the same checkout see unmutated code. The mutant arrives as JSON in
11 * `TANSTACK_DB_MUTANT`; each transform that applies it appends to the file in
12 * `TANSTACK_DB_MUTANT_MARK`, so the harness can tell a surviving mutant from
13 * one that never reached the code.
14 */
15function mutantPlugin(): Plugin {
16 const raw = process.env.TANSTACK_DB_MUTANT
17 const mutant = raw
18 ? (JSON.parse(raw) as { file: string; find: string; replace: string })
19 : undefined
20 const target = mutant ? resolve(__dirname, `../..`, mutant.file) : undefined
21 return {
22 name: `tanstack-db-mutant`,
23 enforce: `pre`,
24 transform(code, id) {
25 if (!mutant || id.split(`?`)[0] !== target) return undefined
26 const count = code.split(mutant.find).length - 1
27 if (count !== 1) {
28 throw new Error(`mutant target occurs ${count} times in ${mutant.file}`)
29 }
30 if (process.env.TANSTACK_DB_MUTANT_MARK) {
31 appendFileSync(process.env.TANSTACK_DB_MUTANT_MARK, `${id}\n`)
32 }
33 return code.replace(mutant.find, () => mutant.replace)
34 },
35 }
37​
38export default mergeConfig(baseConfig, {
39 plugins: [mutantPlugin()],
40 test: {
41 coverage: { enabled: false },
42 typecheck: { enabled: false },
43 },
44})