Skip to content

Commit

Permalink
feat: expose cli start api
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 22, 2022
1 parent 66cf400 commit 3b5371c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 64 deletions.
68 changes: 4 additions & 64 deletions 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')

Expand Down Expand Up @@ -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
Expand All @@ -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()
}
75 changes: 75 additions & 0 deletions 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()
}
}
1 change: 1 addition & 0 deletions 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'

0 comments on commit 3b5371c

Please sign in to comment.