Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: return context in startVitest() #2072

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions packages/vitest/src/node/cli-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<Vitest | undefined> {
process.env.TEST = 'true'
process.env.VITEST = 'true'
process.env.NODE_ENV ??= options.mode || 'test'
Expand All @@ -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')
Expand All @@ -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
}
}
}
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
}
8 changes: 5 additions & 3 deletions packages/vitest/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 3 additions & 0 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
6 changes: 3 additions & 3 deletions test/css/testing.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down