forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli-api.ts
89 lines (73 loc) · 2.48 KB
/
cli-api.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { resolve } from 'pathe'
import type { UserConfig as ViteUserConfig } from 'vite'
import { EXIT_CODE_RESTART } from '../constants'
import { CoverageProviderMap } from '../integrations/coverage'
import { envPackageNames } from '../integrations/env'
import type { UserConfig, VitestRunMode } from '../types'
import { ensurePackageInstalled } from '../utils'
import { createVitest } from './create'
import { registerConsoleShortcuts } from './stdin'
export interface CliOptions extends UserConfig {
/**
* Override the watch mode
*/
run?: boolean
}
export async function startVitest(mode: VitestRunMode, cliFilters: string[], options: CliOptions, viteOverrides?: ViteUserConfig) {
process.env.TEST = 'true'
process.env.VITEST = 'true'
process.env.NODE_ENV ??= options.mode || 'test'
if (options.run)
options.watch = false
if (options.browser) // enabling threads in browser mode causes inconsistences
options.threads = false
// this shouldn't affect _application root_ that can be changed inside config
const root = resolve(options.root || process.cwd())
if (!await ensurePackageInstalled('vite', root)) {
process.exitCode = 1
return false
}
if (typeof options.coverage === 'boolean')
options.coverage = { enabled: options.coverage }
const ctx = await createVitest(mode, options, viteOverrides)
if (mode !== 'benchmark' && ctx.config.coverage.enabled) {
const provider = ctx.config.coverage.provider || 'c8'
if (typeof provider === 'string') {
const requiredPackages = CoverageProviderMap[provider]
if (!await ensurePackageInstalled(requiredPackages, root)) {
process.exitCode = 1
return false
}
}
}
if (ctx.config.environment && ctx.config.environment !== 'node') {
const packageName = envPackageNames[ctx.config.environment]
if (!await ensurePackageInstalled(packageName, root)) {
process.exitCode = 1
return false
}
}
if (process.stdin.isTTY && ctx.config.watch)
registerConsoleShortcuts(ctx)
ctx.onServerRestart((reason) => {
ctx.report('onServerRestart', reason)
if (process.env.VITEST_CLI_WRAPPER)
process.exit(EXIT_CODE_RESTART)
else
ctx.start(cliFilters)
})
try {
await ctx.start(cliFilters)
}
catch (e) {
process.exitCode = 1
await ctx.logger.printError(e, true, 'Unhandled Error')
ctx.logger.error('\n\n')
return false
}
if (!ctx.config.watch) {
await ctx.exit()
return !process.exitCode
}
return true
}