From 22cb31e7c674bfa03f90eb547e4d3f58baa31fae Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 22 Sep 2022 12:41:27 +0800 Subject: [PATCH] feat!: return context in `startVitest()` --- packages/vitest/src/node/cli-api.ts | 32 +++++++++++++++++++---------- packages/vitest/src/node/cli.ts | 8 +++++--- packages/vitest/src/node/core.ts | 3 +++ test/css/testing.mjs | 6 +++--- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/packages/vitest/src/node/cli-api.ts b/packages/vitest/src/node/cli-api.ts index 2058902e8c5d..184c6ed7d60c 100644 --- a/packages/vitest/src/node/cli-api.ts +++ b/packages/vitest/src/node/cli-api.ts @@ -3,7 +3,7 @@ import type { UserConfig as ViteUserConfig } from 'vite' import { EXIT_CODE_RESTART } from '../constants' import { CoverageProviderMap } from '../integrations/coverage' import { getEnvPackageName } from '../integrations/env' -import type { UserConfig, VitestRunMode } from '../types' +import type { UserConfig, Vitest, VitestRunMode } from '../types' import { ensurePackageInstalled } from '../utils' import { createVitest } from './create' import { registerConsoleShortcuts } from './stdin' @@ -15,7 +15,17 @@ export interface CliOptions extends UserConfig { run?: boolean } -export async function startVitest(mode: VitestRunMode, cliFilters: string[], options: CliOptions, viteOverrides?: ViteUserConfig) { +/** + * Start Vitest programmatically + * + * Returns a Vitest instance if initialized successfully. + */ +export async function startVitest( + mode: VitestRunMode, + cliFilters: string[], + options: CliOptions, + viteOverrides?: ViteUserConfig, +): Promise { process.env.TEST = 'true' process.env.VITEST = 'true' process.env.NODE_ENV ??= options.mode || 'test' @@ -30,7 +40,7 @@ export async function startVitest(mode: VitestRunMode, cliFilters: string[], opt if (!await ensurePackageInstalled('vite', root)) { process.exitCode = 1 - return false + return } if (typeof options.coverage === 'boolean') @@ -45,7 +55,7 @@ export async function startVitest(mode: VitestRunMode, cliFilters: string[], opt if (!await ensurePackageInstalled(requiredPackages, root)) { process.exitCode = 1 - return false + return ctx } } } @@ -54,7 +64,7 @@ export async function startVitest(mode: VitestRunMode, cliFilters: string[], opt if (environmentPackage && !await ensurePackageInstalled(environmentPackage, root)) { process.exitCode = 1 - return false + return ctx } if (process.stdin.isTTY && ctx.config.watch) @@ -63,6 +73,7 @@ export async function startVitest(mode: VitestRunMode, cliFilters: string[], opt ctx.onServerRestart((reason) => { ctx.report('onServerRestart', reason) + // if it's in a CLI wrapper, exit with a special code to request restart if (process.env.VITEST_CLI_WRAPPER) process.exit(EXIT_CODE_RESTART) else @@ -76,13 +87,12 @@ export async function startVitest(mode: VitestRunMode, cliFilters: string[], opt process.exitCode = 1 await ctx.logger.printError(e, true, 'Unhandled Error') ctx.logger.error('\n\n') - return false + return ctx } - if (!ctx.config.watch) { - await ctx.exit() - return !process.exitCode - } + if (ctx.config.watch) + return ctx - return true + await ctx.close() + return ctx } diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index 6b9edd92891d..a282a20a15f5 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -94,13 +94,15 @@ async function benchmark(cliFilters: string[], options: CliOptions) { async function start(mode: VitestRunMode, cliFilters: string[], options: CliOptions) { try { - if (await startVitest(mode, cliFilters, options) === false) - process.exit() + const ctx = await startVitest(mode, cliFilters, options) + if (!ctx?.config.watch) + await ctx?.exit() + return ctx } catch (e) { - process.exitCode = 1 console.error(`\n${c.red(divider(c.bold(c.inverse(' Unhandled Error '))))}`) console.error(e) console.error('\n\n') + process.exit(1) } } diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index ccb717370843..17ac9331ffaa 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -475,6 +475,9 @@ export class Vitest { return this.closingPromise } + /** + * Close the thread pool and exit the process + */ async exit(force = false) { setTimeout(() => { console.warn(`close timed out after ${this.config.teardownTimeout}ms`) diff --git a/test/css/testing.mjs b/test/css/testing.mjs index e3e2202cc300..3a4f58b0ee89 100644 --- a/test/css/testing.mjs +++ b/test/css/testing.mjs @@ -10,15 +10,15 @@ const configs = [ async function runTests() { for (const [name, config] of configs) { - const success = await startVitest('test', [name], { + await startVitest('test', [name], { run: true, css: config, update: false, teardownTimeout: 1000_000_000, }) - if (!success) - process.exit(1) + if (process.exitCode) + process.exit() } process.exit(0)