-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathcheck-tests.ts
More file actions
51 lines (44 loc) · 1.44 KB
/
check-tests.ts
File metadata and controls
51 lines (44 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Script to check test files in the codebase.
// - Avoid focused tests (test.only()) that developer could forget to unfocus
// - Avoid skipped tests (test.skip())
// - Ensure tests have describe() blocks
import { globSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { join, relative } from 'node:path'
import { styleText } from 'node:util'
const ROOT = join(import.meta.dirname, '..')
function check(
all: Buffer,
part: string,
filename: string,
message: string
): void {
if (all.includes(part)) {
let lines = all.toString().split('\n')
let line = lines.findIndex(i => i.includes(part)) + 1
let path = relative(ROOT, filename)
process.stderr.write(styleText('red', `${path}:${line} ${message}\n`))
process.exit(1)
}
}
function checkMissing(
all: Buffer,
part: string,
filename: string,
message: string
): void {
if (!all.includes(part)) {
let path = relative(ROOT, filename)
process.stderr.write(styleText('red', `${path} ${message}\n`))
process.exit(1)
}
}
async function checkFile(filename: string): Promise<void> {
let code = await readFile(filename)
check(code, 'test.only(', filename, 'has focused test')
check(code, 'test.skip(', filename, 'has skipped test')
checkMissing(code, 'describe(', filename, 'missing describe() block')
}
let files =
process.argv.length > 2 ? process.argv.slice(2) : globSync('**/*.test.ts')
await Promise.all(files.map(file => checkFile(file)))