[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(api): don't call process.exit manually (#5926)

authored by

Vladimir and committed by
GitHub
(Jul 1, 2024, 9:27 AM +0200) e9b638d4 169bc1fd

+42 -18
-2
packages/vitest/src/constants.ts
··· 3 3 export const defaultBrowserPort = 63315 4 4 export const defaultInspectPort = 9229 5 5 6 - export const EXIT_CODE_RESTART = 43 7 - 8 6 export const API_PATH = '/__vitest_api__' 9 7 10 8 export const extraInlineDeps = [
+1 -1
test/config/test/mode.test.ts
··· 17 17 const { stdout, stderr } = await testUtils.runVitestCli(...(command), 'fixtures/mode', '-c', `fixtures/mode/vitest.${expectedMode}.config.ts`) 18 18 19 19 expect(stderr).toContain(`env.mode: ${actualMode}`) 20 - expect(stderr).toContain('⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯') 20 + expect(stderr).toContain('Startup Error') 21 21 expect(stderr).toContain(`Error: env.mode should be equal to "${expectedMode}"`) 22 22 expect(stdout).toBe('') 23 23 })
+5 -4
packages/vitest/src/node/core.ts
··· 6 6 import { basename, dirname, join, normalize, relative, resolve } from 'pathe' 7 7 import fg from 'fast-glob' 8 8 import mm from 'micromatch' 9 - import c from 'picocolors' 10 9 import { ViteNodeRunner } from 'vite-node/client' 11 10 import { SnapshotManager } from '@vitest/snapshot/manager' 12 11 import type { CancelReason, File, TaskResultPack } from '@vitest/runner' ··· 29 28 import { WorkspaceProject, initializeProject } from './workspace' 30 29 import { VitestPackageInstaller } from './packageInstaller' 31 30 import { BlobReporter, readBlobs } from './reporters/blob' 31 + import { FilesNotFoundError, GitNotFoundError } from './errors' 32 32 33 33 const WATCHER_DEBOUNCE = 100 34 34 ··· 492 492 493 493 if (!this.config.watch || !(this.config.changed || this.config.related?.length)) { 494 494 const exitCode = this.config.passWithNoTests ? 0 : 1 495 - process.exit(exitCode) 495 + process.exitCode = exitCode 496 + throw new FilesNotFoundError(this.mode) 496 497 } 497 498 } 498 499 ··· 564 565 changedSince: this.config.changed, 565 566 }) 566 567 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() 569 570 } 570 571 this.config.related = Array.from(new Set(related)) 571 572 }
+15
packages/vitest/src/node/errors.ts
··· 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 + }
+2
packages/vitest/src/node/index.ts
··· 14 14 export { resolveFsAllow } from './plugins/utils' 15 15 export { resolveApiServerConfig, resolveConfig } from './config' 16 16 17 + export { GitNotFoundError, FilesNotFoundError as TestsNotFoundError } from './errors' 18 + 17 19 export { distDir, rootDir } from '../paths' 18 20 19 21 export type {
+1 -2
packages/vitest/src/node/packageInstaller.ts
··· 2 2 import { createRequire } from 'node:module' 3 3 import c from 'picocolors' 4 4 import { isPackageExists } from 'local-pkg' 5 - import { EXIT_CODE_RESTART } from '../constants' 6 5 import { isCI } from '../utils/env' 7 6 8 7 const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) ··· 59 58 `\nPackage ${dependency} installed, re-run the command to start.\n`, 60 59 ), 61 60 ) 62 - process.exit(EXIT_CODE_RESTART) 61 + process.exit() 63 62 return true 64 63 } 65 64
+7 -2
packages/vitest/src/node/cli/cac.ts
··· 265 265 } 266 266 catch (e) { 267 267 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 '))))}`) 269 269 console.error(e) 270 270 console.error('\n\n') 271 - process.exit(1) 271 + 272 + if (process.exitCode == null) { 273 + process.exitCode = 1 274 + } 275 + 276 + process.exit() 272 277 } 273 278 } 274 279
+10 -6
packages/vitest/src/node/cli/cli-api.ts
··· 1 1 import { resolve } from 'pathe' 2 2 import type { UserConfig as ViteUserConfig } from 'vite' 3 - import { EXIT_CODE_RESTART } from '../../constants' 4 3 import { CoverageProviderMap } from '../../integrations/coverage' 5 4 import { getEnvPackageName } from '../../integrations/env' 6 5 import type { UserConfig, Vitest, VitestRunMode } from '../../types' 7 6 import { createVitest } from '../create' 8 7 import { registerConsoleShortcuts } from '../stdin' 9 8 import type { VitestOptions } from '../core' 9 + import { FilesNotFoundError, GitNotFoundError } from '../errors' 10 10 11 11 export interface CliOptions extends UserConfig { 12 12 /** ··· 86 86 87 87 ctx.onServerRestart((reason) => { 88 88 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 - } 94 89 }) 95 90 96 91 ctx.onAfterSetServer(() => { ··· 114 109 } 115 110 } 116 111 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 + 117 121 process.exitCode = 1 118 122 ctx.logger.printError(e, { fullStack: true, type: 'Unhandled Error' }) 119 123 ctx.logger.error('\n\n')
+1 -1
packages/vitest/src/node/reporters/base.ts
··· 625 625 type: 'Unhandled Rejection', 626 626 }) 627 627 this.ctx.logger.error('\n\n') 628 - process.exit(1) 628 + process.exit() 629 629 } 630 630 process.on('unhandledRejection', onUnhandledRejection) 631 631 this._offUnhandledRejection = () => {