Open History to see the last commit
1import { spawnSync } from 'node:child_process'
2import {
3 existsSync,
4 mkdtempSync,
5 mkdirSync,
6 readFileSync,
7 rmSync,
8 writeFileSync,
9} from 'node:fs'
10import { tmpdir } from 'node:os'
11import { join } from 'node:path'
12import { fileURLToPath } from 'node:url'
13import { afterEach, describe, expect, it } from 'vitest'
14
15const SCRIPT = fileURLToPath(new URL('./coverage-check.mjs', import.meta.url))
16
17const temps: Array<string> = []
18
19afterEach(() => {
20 for (const dir of temps.splice(0))
21 rmSync(dir, { recursive: true, force: true })
22})
23
24function tmp() {
25 const dir = mkdtempSync(join(tmpdir(), 'cov-check-'))
26 temps.push(dir)
27 return dir
28}
29
30function run(
31 args: Array<string>,
32 opts: { cwd?: string; env?: NodeJS.ProcessEnv } = {},
33) {
34 const env: NodeJS.ProcessEnv = { ...process.env, ...opts.env }
35 if (!opts.env || !('GITHUB_STEP_SUMMARY' in opts.env)) {
36 delete env.GITHUB_STEP_SUMMARY
37 }
38 return spawnSync(process.execPath, [SCRIPT, ...args], {
39 encoding: 'utf8',
40 cwd: opts.cwd,
41 env,
42 })
43}
44
45function metrics(pct: number) {
46 return { statements: pct, branches: pct, functions: pct, lines: pct }
47}
48
49function writeSnapshot(dir: string, name: string, pct: number) {
50 mkdirSync(dir, { recursive: true })
51 writeFileSync(join(dir, `${name}.json`), JSON.stringify(metrics(pct)))
52}
53
54function vitestSummary(pct: number, total = 10) {
55 const cell = { total, covered: 1, pct }
56 return JSON.stringify({
57 total: {
58 statements: cell,
59 branches: cell,
60 functions: cell,
61 lines: cell,
62 },
63 })
64}
65
66function writePkg(
67 root: string,
68 dirName: string,
69 summary: string,
70 pkgName = `@tanstack/${dirName}`,
71) {
72 mkdirSync(join(root, 'packages', dirName, 'coverage'), { recursive: true })
73 writeFileSync(
74 join(root, 'packages', dirName, 'package.json'),
75 JSON.stringify({ name: pkgName }),
76 )
77 writeFileSync(
78 join(root, 'packages', dirName, 'coverage', 'coverage-summary.json'),
79 summary,
80 )
81}
82
83describe('usage', () => {
84 it('exits 2 when --base or --head is missing', () => {
85 const r = run([])
86 expect(r.status).toBe(2)
87 expect(r.stderr).toMatch(/Usage:/)
88 })
89})
90
91describe('compare', () => {
92 it('passes a 0.4pp drop and a 0.5pp drop, fails 0.51pp', () => {
93 const base = tmp()
94 const head = tmp()
95 writeSnapshot(base, 'ok', 90)
96 writeSnapshot(head, 'ok', 89.5)
97 expect(run(['--base', base, '--head', head]).status).toBe(0)
98
99 writeSnapshot(head, 'ok', 89.6)
100 expect(run(['--base', base, '--head', head]).status).toBe(0)
101
102 writeSnapshot(head, 'ok', 89.49)
103 const r = run(['--base', base, '--head', head])
104 expect(r.status).toBe(1)
105 expect(r.stderr).toMatch(/ok statements: 90\.00% -> 89\.49%/)
106 expect(r.stdout).toMatch(/DROP/)
107 })
108
109 it('fails when only branches drop more than 0.5pp', () => {
110 const base = tmp()
111 const head = tmp()
112 mkdirSync(base, { recursive: true })
113 mkdirSync(head, { recursive: true })
114 writeFileSync(
115 join(base, 'pkg.json'),
116 JSON.stringify({
117 statements: 90,
118 branches: 80,
119 functions: 90,
120 lines: 90,
121 }),
122 )
123 writeFileSync(
124 join(head, 'pkg.json'),
125 JSON.stringify({
126 statements: 90,
127 branches: 79,
128 functions: 90,
129 lines: 90,
130 }),
131 )
132 const r = run(['--base', base, '--head', head])
133 expect(r.status).toBe(1)
134 expect(r.stderr).toMatch(/pkg branches: 80\.00% -> 79\.00%/)
135 expect(r.stderr).not.toMatch(/statements/)
136 })
137
138 it('treats a head-only package as new and exits 0', () => {
139 const base = tmp()
140 const head = tmp()
141 mkdirSync(base, { recursive: true })
142 writeSnapshot(head, 'ai-new', 12)
143 const r = run(['--base', base, '--head', head])
144 expect(r.status).toBe(0)
145 expect(r.stdout).toMatch(/\bnew\b/)
146 expect(r.stdout).toMatch(/nothing to compare against/)
147 expect(r.stdout).not.toMatch(/Coverage held/)
148 })
149
150 it('fails when a package was measured on base but not on head', () => {
151 const base = tmp()
152 const head = tmp()
153 writeSnapshot(base, 'ai-old', 90)
154 writeSnapshot(head, 'other', 90)
155 const r = run(['--base', base, '--head', head])
156 expect(r.status).toBe(1)
157 expect(r.stderr).toMatch(/ai-old: measured on base, missing on this PR/)
158 })
159
160 it('fails when head measured nothing', () => {
161 const base = tmp()
162 const head = tmp()
163 mkdirSync(base, { recursive: true })
164 mkdirSync(head, { recursive: true })
165 const r = run(['--base', base, '--head', head])
166 expect(r.status).toBe(1)
167 expect(r.stderr).toMatch(/nothing to compare/)
168 })
169
170 it('writes the step summary on a failing run', () => {
171 const base = tmp()
172 const head = tmp()
173 const summary = join(tmp(), 'summary.md')
174 writeSnapshot(base, 'pkg', 90)
175 writeSnapshot(head, 'pkg', 80)
176 const r = run(['--base', base, '--head', head], {
177 env: { ...process.env, GITHUB_STEP_SUMMARY: summary },
178 })
179 expect(r.status).toBe(1)
180 const md = readFileSync(summary, 'utf8')
181 expect(md).toMatch(/Coverage dropped/)
182 expect(md).toMatch(/pkg/)
183 })
184})
185
186describe('collect', () => {
187 it('writes pct-only snapshots and skips packages with no summary', () => {
188 const root = tmp()
189 const out = join(root, 'out')
190 writePkg(root, 'ai', vitestSummary(90))
191 mkdirSync(join(root, 'packages', 'empty'), { recursive: true })
192 writeFileSync(
193 join(root, 'packages', 'empty', 'package.json'),
194 JSON.stringify({ name: '@tanstack/empty' }),
195 )
196 const r = run(['--collect', out], { cwd: root })
197 expect(r.status).toBe(0)
198 const written = JSON.parse(readFileSync(join(out, 'ai.json'), 'utf8'))
199 expect(written).toEqual(metrics(90))
200 expect(r.stdout).toMatch(/Collected 1 /)
201 })
202
203 it('fails --expect when a named package has no summary', () => {
204 const root = tmp()
205 const out = join(root, 'out')
206 writePkg(root, 'ai', vitestSummary(90))
207 const r = run(['--collect', out, '--expect', 'ai,missing-pkg'], {
208 cwd: root,
209 })
210 expect(r.status).toBe(1)
211 expect(r.stderr).toMatch(/missing-pkg/)
212 })
213
214 it('fails --expect when statements.total is 0', () => {
215 const root = tmp()
216 const out = join(root, 'out')
217 writePkg(root, 'empty', vitestSummary(100, 0))
218 const r = run(['--collect', out, '--expect', 'empty'], { cwd: root })
219 expect(r.status).toBe(1)
220 expect(r.stderr).toMatch(/0 statements/)
221 })
222
223 it('resolves @tanstack scoped names to the package directory', () => {
224 const root = tmp()
225 const out = join(root, 'out')
226 writePkg(
227 root,
228 'ai-devtools',
229 vitestSummary(81),
230 '@tanstack/ai-devtools-core',
231 )
232 const r = run(
233 ['--collect', out, '--expect', '@tanstack/ai-devtools-core'],
234 { cwd: root },
235 )
236 expect(r.status).toBe(0)
237 expect(existsSync(join(out, 'ai-devtools.json'))).toBe(true)
238 })
239})