From 3b5371c03e40f8a695b1a86c04d4d6bae26ff5cb Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 22 Feb 2022 13:52:48 +0800 Subject: [PATCH] feat: expose cli start api --- packages/vitest/src/node/cli.ts | 68 ++------------------------- packages/vitest/src/node/cliApi.ts | 75 ++++++++++++++++++++++++++++++ packages/vitest/src/node/index.ts | 1 + 3 files changed, 80 insertions(+), 64 deletions(-) create mode 100644 packages/vitest/src/node/cliApi.ts diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index 60eff37df25f..6e38d56e0b0c 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -1,10 +1,7 @@ import cac from 'cac' -import { execa } from 'execa' -import type { UserConfig } from '../types' import { version } from '../../package.json' -import { ensurePackageInstalled } from '../utils' -import { createVitest } from './create' -import { registerConsoleShortcuts } from './stdin' +import type { CliOptions } from './cliApi' +import { startVitest } from './cliApi' const cli = cac('vitest') @@ -56,13 +53,6 @@ cli cli.parse() -export interface CliOptions extends UserConfig { - /** - * Override the watch mode - */ - run?: boolean -} - async function runRelated(relatedFiles: string[] | string, argv: CliOptions) { argv.related = relatedFiles argv.passWithNoTests ??= true @@ -75,56 +65,6 @@ async function run(cliFilters: string[], options: CliOptions) { } async function start(cliFilters: string[], options: CliOptions) { - 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.exit(1) - - if (typeof options.coverage === 'boolean') - options.coverage = { enabled: options.coverage } - - const ctx = await createVitest(options) - - if (ctx.config.coverage.enabled) { - if (!await ensurePackageInstalled('c8')) - process.exit(1) - - 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.exit(exitCode) - } - } - - if (ctx.config.environment && ctx.config.environment !== 'node') { - if (!await ensurePackageInstalled(ctx.config.environment)) - process.exit(1) - } - - 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() - } + await startVitest(cliFilters, options) + process.exit() } diff --git a/packages/vitest/src/node/cliApi.ts b/packages/vitest/src/node/cliApi.ts new file mode 100644 index 000000000000..154d800e676a --- /dev/null +++ b/packages/vitest/src/node/cliApi.ts @@ -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() + } +} diff --git a/packages/vitest/src/node/index.ts b/packages/vitest/src/node/index.ts index 5a492af4ce05..91c96f478411 100644 --- a/packages/vitest/src/node/index.ts +++ b/packages/vitest/src/node/index.ts @@ -1,3 +1,4 @@ export type { Vitest } from './core' export { createVitest } from './create' export { VitestPlugin } from './plugins' +export { startVitest } from './cliAPI'