-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
61 lines (54 loc) · 1.42 KB
/
test.ts
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
52
53
54
55
56
57
58
59
60
61
import { exec } from 'exec'
import glob from 'fast-glob'
import type { CommonOptions } from './cli/options'
import { getEnv } from './env'
import { debug } from './util/debug'
import { stdio } from './util/stdio'
export async function startTestRunner(
globs: string[],
{ dir, env, ...flags }: Record<string, any> & CommonOptions,
) {
env ??= getEnv(dir)
// The "vitest run" command disables watch mode.
let subCommand: string | undefined
if (globs[0] === 'run') {
subCommand = 'run'
}
const args = [
'-s',
'vitest',
subCommand,
'--coverage',
...globs,
...arrifyFlags(flags),
]
// If globs are passed, only check coverage for the files that match
// the globs.
if (globs.length > 0) {
const globbishRE = /(^src\b|\*)/
const files = await glob(
globs.map(glob => (globbishRE.test(glob) ? glob : `src/**/${glob}*`)),
{
cwd: process.cwd(),
},
)
for (const file of files) {
args.push('--coverage.include', file)
}
}
debug('Running:', ['pnpm', ...args])
await exec('pnpm', args, {
cwd: env.root,
stdio,
})
}
function arrifyFlags(flags: Record<string, any>) {
return Object.entries(flags).flatMap(([key, value]) => {
if (key === '--') {
return []
}
const name = value === false ? 'no-' + key : key
const flag = name.length === 1 ? `-${name}` : `--${name}`
return value === true ? [flag] : [flag, value]
})
}