Skip to content

Commit e9b638d

Browse files
authored
fix(api): don't call process.exit manually (#5926)
1 parent 169bc1f commit e9b638d

File tree

9 files changed

+42
-18
lines changed

9 files changed

+42
-18
lines changed

packages/vitest/src/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ export const defaultPort = 51204
33
export const defaultBrowserPort = 63315
44
export const defaultInspectPort = 9229
55

6-
export const EXIT_CODE_RESTART = 43
7-
86
export const API_PATH = '/__vitest_api__'
97

108
export const extraInlineDeps = [

packages/vitest/src/node/cli/cac.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,15 @@ async function start(mode: VitestRunMode, cliFilters: string[], options: CliOpti
265265
}
266266
catch (e) {
267267
const { divider } = await import('../reporters/renderers/utils')
268-
console.error(`\n${c.red(divider(c.bold(c.inverse(' Unhandled Error '))))}`)
268+
console.error(`\n${c.red(divider(c.bold(c.inverse(' Startup Error '))))}`)
269269
console.error(e)
270270
console.error('\n\n')
271-
process.exit(1)
271+
272+
if (process.exitCode == null) {
273+
process.exitCode = 1
274+
}
275+
276+
process.exit()
272277
}
273278
}
274279

packages/vitest/src/node/cli/cli-api.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { resolve } from 'pathe'
22
import type { UserConfig as ViteUserConfig } from 'vite'
3-
import { EXIT_CODE_RESTART } from '../../constants'
43
import { CoverageProviderMap } from '../../integrations/coverage'
54
import { getEnvPackageName } from '../../integrations/env'
65
import type { UserConfig, Vitest, VitestRunMode } from '../../types'
76
import { createVitest } from '../create'
87
import { registerConsoleShortcuts } from '../stdin'
98
import type { VitestOptions } from '../core'
9+
import { FilesNotFoundError, GitNotFoundError } from '../errors'
1010

1111
export interface CliOptions extends UserConfig {
1212
/**
@@ -86,11 +86,6 @@ export async function startVitest(
8686

8787
ctx.onServerRestart((reason) => {
8888
ctx.report('onServerRestart', reason)
89-
90-
// if it's in a CLI wrapper, exit with a special code to request restart
91-
if (process.env.VITEST_CLI_WRAPPER) {
92-
process.exit(EXIT_CODE_RESTART)
93-
}
9489
})
9590

9691
ctx.onAfterSetServer(() => {
@@ -114,6 +109,15 @@ export async function startVitest(
114109
}
115110
}
116111
catch (e) {
112+
if (e instanceof FilesNotFoundError) {
113+
return ctx
114+
}
115+
116+
if (e instanceof GitNotFoundError) {
117+
ctx.logger.error(e.message)
118+
return ctx
119+
}
120+
117121
process.exitCode = 1
118122
ctx.logger.printError(e, { fullStack: true, type: 'Unhandled Error' })
119123
ctx.logger.error('\n\n')

packages/vitest/src/node/core.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { mergeConfig } from 'vite'
66
import { basename, dirname, join, normalize, relative, resolve } from 'pathe'
77
import fg from 'fast-glob'
88
import mm from 'micromatch'
9-
import c from 'picocolors'
109
import { ViteNodeRunner } from 'vite-node/client'
1110
import { SnapshotManager } from '@vitest/snapshot/manager'
1211
import type { CancelReason, File, TaskResultPack } from '@vitest/runner'
@@ -29,6 +28,7 @@ import { VitestCache } from './cache'
2928
import { WorkspaceProject, initializeProject } from './workspace'
3029
import { VitestPackageInstaller } from './packageInstaller'
3130
import { BlobReporter, readBlobs } from './reporters/blob'
31+
import { FilesNotFoundError, GitNotFoundError } from './errors'
3232

3333
const WATCHER_DEBOUNCE = 100
3434

@@ -492,7 +492,8 @@ export class Vitest {
492492

493493
if (!this.config.watch || !(this.config.changed || this.config.related?.length)) {
494494
const exitCode = this.config.passWithNoTests ? 0 : 1
495-
process.exit(exitCode)
495+
process.exitCode = exitCode
496+
throw new FilesNotFoundError(this.mode)
496497
}
497498
}
498499

@@ -564,8 +565,8 @@ export class Vitest {
564565
changedSince: this.config.changed,
565566
})
566567
if (!related) {
567-
this.logger.error(c.red('Could not find Git root. Have you initialized git with `git init`?\n'))
568-
process.exit(1)
568+
process.exitCode = 1
569+
throw new GitNotFoundError()
569570
}
570571
this.config.related = Array.from(new Set(related))
571572
}

packages/vitest/src/node/errors.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class FilesNotFoundError extends Error {
2+
code = 'VITEST_FILES_NOT_FOUND'
3+
4+
constructor(mode: 'test' | 'benchmark') {
5+
super(`No ${mode} files found`)
6+
}
7+
}
8+
9+
export class GitNotFoundError extends Error {
10+
code = 'VITEST_GIT_NOT_FOUND'
11+
12+
constructor() {
13+
super('Could not find Git root. Have you initialized git with `git init`?')
14+
}
15+
}

packages/vitest/src/node/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export { createDebugger } from '../utils/debugger'
1414
export { resolveFsAllow } from './plugins/utils'
1515
export { resolveApiServerConfig, resolveConfig } from './config'
1616

17+
export { GitNotFoundError, FilesNotFoundError as TestsNotFoundError } from './errors'
18+
1719
export { distDir, rootDir } from '../paths'
1820

1921
export type {

packages/vitest/src/node/packageInstaller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import url from 'node:url'
22
import { createRequire } from 'node:module'
33
import c from 'picocolors'
44
import { isPackageExists } from 'local-pkg'
5-
import { EXIT_CODE_RESTART } from '../constants'
65
import { isCI } from '../utils/env'
76

87
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
@@ -59,7 +58,7 @@ export class VitestPackageInstaller {
5958
`\nPackage ${dependency} installed, re-run the command to start.\n`,
6059
),
6160
)
62-
process.exit(EXIT_CODE_RESTART)
61+
process.exit()
6362
return true
6463
}
6564

packages/vitest/src/node/reporters/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ export abstract class BaseReporter implements Reporter {
625625
type: 'Unhandled Rejection',
626626
})
627627
this.ctx.logger.error('\n\n')
628-
process.exit(1)
628+
process.exit()
629629
}
630630
process.on('unhandledRejection', onUnhandledRejection)
631631
this._offUnhandledRejection = () => {

test/config/test/mode.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test.each([
1717
const { stdout, stderr } = await testUtils.runVitestCli(...(command), 'fixtures/mode', '-c', `fixtures/mode/vitest.${expectedMode}.config.ts`)
1818

1919
expect(stderr).toContain(`env.mode: ${actualMode}`)
20-
expect(stderr).toContain('⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯')
20+
expect(stderr).toContain('Startup Error')
2121
expect(stderr).toContain(`Error: env.mode should be equal to "${expectedMode}"`)
2222
expect(stdout).toBe('')
2323
})

0 commit comments

Comments
 (0)