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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 21 additions & 11 deletions packages/vitest/src/node/cli-api.ts
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
}
12 changes: 7 additions & 5 deletions packages/vitest/src/node/cli.ts
Expand Up @@ -2,7 +2,7 @@ import { normalize } from 'pathe'
import cac from 'cac'
import c from 'picocolors'
import { version } from '../../package.json'
import type { VitestRunMode } from '../types'
import type { Vitest, VitestRunMode } from '../types'
import type { CliOptions } from './cli-api'
import { startVitest } from './cli-api'
import { divider } from './reporters/renderers/utils'
Expand Down Expand Up @@ -100,15 +100,17 @@ function normalizeOptions(argv: CliOptions): CliOptions {
return argv
}

async function start(mode: VitestRunMode, cliFilters: string[], options: CliOptions): Promise<void> {
async function start(mode: VitestRunMode, cliFilters: string[], options: CliOptions): Promise<Vitest | undefined> {
try {
if (await startVitest(mode, cliFilters.map(normalize), normalizeOptions(options)) === false)
process.exit()
const ctx = await startVitest(mode, cliFilters.map(normalize), normalizeOptions(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
Expand Up @@ -479,6 +479,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
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