(
mode: VitestRunMode,
cliFilters: string[] = [],
options: CliOptions = {},
viteOverrides?: ViteUserConfig,
vitestOptions?: VitestOptions,
)
| 54 | * Returns a Vitest instance if initialized successfully. |
| 55 | */ |
| 56 | export async function startVitest( |
| 57 | mode: VitestRunMode, |
| 58 | cliFilters: string[] = [], |
| 59 | options: CliOptions = {}, |
| 60 | viteOverrides?: ViteUserConfig, |
| 61 | vitestOptions?: VitestOptions, |
| 62 | ): Promise<Vitest> { |
| 63 | const root = resolve(options.root || process.cwd()) |
| 64 | |
| 65 | const ctx = await prepareVitest( |
| 66 | mode, |
| 67 | options, |
| 68 | viteOverrides, |
| 69 | vitestOptions, |
| 70 | cliFilters, |
| 71 | ) |
| 72 | |
| 73 | if (mode === 'test' && ctx._coverageOptions.enabled) { |
| 74 | const provider = ctx._coverageOptions.provider || 'v8' |
| 75 | const requiredPackages = CoverageProviderMap[provider] |
| 76 | |
| 77 | if (requiredPackages) { |
| 78 | if ( |
| 79 | !(await ctx.packageInstaller.ensureInstalled(requiredPackages, root, ctx.version)) |
| 80 | ) { |
| 81 | process.exitCode = 1 |
| 82 | return ctx |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | const stdin = vitestOptions?.stdin || process.stdin |
| 88 | const stdout = vitestOptions?.stdout || process.stdout |
| 89 | let stdinCleanup |
| 90 | if (stdin.isTTY && ctx.config.watch) { |
| 91 | stdinCleanup = registerConsoleShortcuts(ctx, stdin, stdout) |
| 92 | } |
| 93 | |
| 94 | ctx.onAfterSetServer(() => { |
| 95 | if (ctx.config.standalone) { |
| 96 | ctx.standalone() |
| 97 | } |
| 98 | else { |
| 99 | ctx.start(cliFilters) |
| 100 | } |
| 101 | }) |
| 102 | |
| 103 | try { |
| 104 | if (ctx.config.listTags) { |
| 105 | await ctx.listTags() |
| 106 | } |
| 107 | else if (ctx.config.clearCache) { |
| 108 | await ctx.experimental_clearCache() |
| 109 | } |
| 110 | else if (ctx.config.mergeReports) { |
| 111 | await ctx.mergeReports() |
| 112 | } |
| 113 | else if (ctx.config.standalone) { |
no test coverage detected