Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { execa } from 'execa' | ||
import type { UserConfig as ViteUserConfig } from 'vite' | ||
import type { UserConfig } 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(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 (!await ensurePackageInstalled('vite')) { | ||
process.exitCode = 1 | ||
return | ||
} | ||
|
||
if (typeof options.coverage === 'boolean') | ||
options.coverage = { enabled: options.coverage } | ||
|
||
const ctx = await createVitest(options, viteOverrides) | ||
|
||
if (ctx.config.coverage.enabled) { | ||
if (!await ensurePackageInstalled('c8')) { | ||
process.exitCode = 1 | ||
return | ||
} | ||
|
||
if (!process.env.NODE_V8_COVERAGE) { | ||
process.env.NODE_V8_COVERAGE = ctx.config.coverage.tempDirectory | ||
const { exitCode } = await execa(process.argv0, process.argv.slice(1), { stdio: 'inherit', reject: false }) | ||
process.exitCode = exitCode | ||
return | ||
} | ||
} | ||
|
||
if (ctx.config.environment && ctx.config.environment !== 'node') { | ||
if (!await ensurePackageInstalled(ctx.config.environment)) { | ||
process.exitCode = 1 | ||
return | ||
} | ||
} | ||
|
||
if (process.stdin.isTTY && ctx.config.watch) | ||
registerConsoleShortcuts(ctx) | ||
|
||
process.chdir(ctx.config.root) | ||
|
||
ctx.onServerRestarted(() => { | ||
// TODO: re-consider how to re-run the tests the server smartly | ||
ctx.start(cliFilters) | ||
}) | ||
|
||
try { | ||
await ctx.start(cliFilters) | ||
} | ||
catch (e) { | ||
process.exitCode = 1 | ||
throw e | ||
} | ||
finally { | ||
if (!ctx.config.watch) | ||
await ctx.exit() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export type { Vitest } from './core' | ||
export { createVitest } from './create' | ||
export { VitestPlugin } from './plugins' | ||
export { startVitest } from './cliAPI' |