Skip to content

Commit

Permalink
fix(api): don't call process.exit manually (#5926)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jul 1, 2024
1 parent 169bc1f commit e9b638d
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 18 deletions.
2 changes: 0 additions & 2 deletions packages/vitest/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ export const defaultPort = 51204
export const defaultBrowserPort = 63315
export const defaultInspectPort = 9229

export const EXIT_CODE_RESTART = 43

export const API_PATH = '/__vitest_api__'

export const extraInlineDeps = [
Expand Down
9 changes: 7 additions & 2 deletions packages/vitest/src/node/cli/cac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,15 @@ async function start(mode: VitestRunMode, cliFilters: string[], options: CliOpti
}
catch (e) {
const { divider } = await import('../reporters/renderers/utils')
console.error(`\n${c.red(divider(c.bold(c.inverse(' Unhandled Error '))))}`)
console.error(`\n${c.red(divider(c.bold(c.inverse(' Startup Error '))))}`)
console.error(e)
console.error('\n\n')
process.exit(1)

if (process.exitCode == null) {
process.exitCode = 1
}

process.exit()
}
}

Expand Down
16 changes: 10 additions & 6 deletions packages/vitest/src/node/cli/cli-api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { resolve } from 'pathe'
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, Vitest, VitestRunMode } from '../../types'
import { createVitest } from '../create'
import { registerConsoleShortcuts } from '../stdin'
import type { VitestOptions } from '../core'
import { FilesNotFoundError, GitNotFoundError } from '../errors'

export interface CliOptions extends UserConfig {
/**
Expand Down Expand Up @@ -86,11 +86,6 @@ export async function startVitest(

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)
}
})

ctx.onAfterSetServer(() => {
Expand All @@ -114,6 +109,15 @@ export async function startVitest(
}
}
catch (e) {
if (e instanceof FilesNotFoundError) {
return ctx
}

if (e instanceof GitNotFoundError) {
ctx.logger.error(e.message)
return ctx
}

process.exitCode = 1
ctx.logger.printError(e, { fullStack: true, type: 'Unhandled Error' })
ctx.logger.error('\n\n')
Expand Down
9 changes: 5 additions & 4 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { mergeConfig } from 'vite'
import { basename, dirname, join, normalize, relative, resolve } from 'pathe'
import fg from 'fast-glob'
import mm from 'micromatch'
import c from 'picocolors'
import { ViteNodeRunner } from 'vite-node/client'
import { SnapshotManager } from '@vitest/snapshot/manager'
import type { CancelReason, File, TaskResultPack } from '@vitest/runner'
Expand All @@ -29,6 +28,7 @@ import { VitestCache } from './cache'
import { WorkspaceProject, initializeProject } from './workspace'
import { VitestPackageInstaller } from './packageInstaller'
import { BlobReporter, readBlobs } from './reporters/blob'
import { FilesNotFoundError, GitNotFoundError } from './errors'

const WATCHER_DEBOUNCE = 100

Expand Down Expand Up @@ -492,7 +492,8 @@ export class Vitest {

if (!this.config.watch || !(this.config.changed || this.config.related?.length)) {
const exitCode = this.config.passWithNoTests ? 0 : 1
process.exit(exitCode)
process.exitCode = exitCode
throw new FilesNotFoundError(this.mode)
}
}

Expand Down Expand Up @@ -564,8 +565,8 @@ export class Vitest {
changedSince: this.config.changed,
})
if (!related) {
this.logger.error(c.red('Could not find Git root. Have you initialized git with `git init`?\n'))
process.exit(1)
process.exitCode = 1
throw new GitNotFoundError()
}
this.config.related = Array.from(new Set(related))
}
Expand Down
15 changes: 15 additions & 0 deletions packages/vitest/src/node/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class FilesNotFoundError extends Error {
code = 'VITEST_FILES_NOT_FOUND'

constructor(mode: 'test' | 'benchmark') {
super(`No ${mode} files found`)
}
}

export class GitNotFoundError extends Error {
code = 'VITEST_GIT_NOT_FOUND'

constructor() {
super('Could not find Git root. Have you initialized git with `git init`?')
}
}
2 changes: 2 additions & 0 deletions packages/vitest/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export { createDebugger } from '../utils/debugger'
export { resolveFsAllow } from './plugins/utils'
export { resolveApiServerConfig, resolveConfig } from './config'

export { GitNotFoundError, FilesNotFoundError as TestsNotFoundError } from './errors'

export { distDir, rootDir } from '../paths'

export type {
Expand Down
3 changes: 1 addition & 2 deletions packages/vitest/src/node/packageInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import url from 'node:url'
import { createRequire } from 'node:module'
import c from 'picocolors'
import { isPackageExists } from 'local-pkg'
import { EXIT_CODE_RESTART } from '../constants'
import { isCI } from '../utils/env'

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
Expand Down Expand Up @@ -59,7 +58,7 @@ export class VitestPackageInstaller {
`\nPackage ${dependency} installed, re-run the command to start.\n`,
),
)
process.exit(EXIT_CODE_RESTART)
process.exit()
return true
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ export abstract class BaseReporter implements Reporter {
type: 'Unhandled Rejection',
})
this.ctx.logger.error('\n\n')
process.exit(1)
process.exit()
}
process.on('unhandledRejection', onUnhandledRejection)
this._offUnhandledRejection = () => {
Expand Down
2 changes: 1 addition & 1 deletion test/config/test/mode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test.each([
const { stdout, stderr } = await testUtils.runVitestCli(...(command), 'fixtures/mode', '-c', `fixtures/mode/vitest.${expectedMode}.config.ts`)

expect(stderr).toContain(`env.mode: ${actualMode}`)
expect(stderr).toContain('⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯')
expect(stderr).toContain('Startup Error')
expect(stderr).toContain(`Error: env.mode should be equal to "${expectedMode}"`)
expect(stdout).toBe('')
})

0 comments on commit e9b638d

Please sign in to comment.