Skip to content

Commit

Permalink
fix(vitest): gracefully exit Vitest if process.exit is called insid…
Browse files Browse the repository at this point in the history
…e the test (#4903)
  • Loading branch information
sheremet-va authored Jan 9, 2024
1 parent 043b78f commit 8e6c104
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 15 deletions.
3 changes: 1 addition & 2 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ export async function printError(error: unknown, project: WorkspaceProject | und
const nearest = error instanceof TypeCheckError
? error.stacks[0]
: stacks.find(stack =>
project.getModuleById(stack.file)
&& existsSync(stack.file),
project.server && project.getModuleById(stack.file) && existsSync(stack.file),
)

const errorProperties = getErrorProperties(e)
Expand Down
4 changes: 0 additions & 4 deletions packages/vitest/src/node/pools/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import type { WorkspaceProject } from '../workspace'
export function createMethodsRPC(project: WorkspaceProject): RuntimeRPC {
const ctx = project.ctx
return {
async onWorkerExit(error, code) {
await ctx.logger.printError(error, { type: 'Unexpected Exit', fullStack: true })
process.exit(code || 1)
},
snapshotSaved(snapshot) {
ctx.snapshot.add(snapshot)
},
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function init(ctx: ChildContext) {
onCancel: setCancel,
},
{
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onWorkerExit', 'onCancel'],
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onCancel'],
serialize: v8.serialize,
deserialize: v => v8.deserialize(Buffer.from(v)),
post(v) {
Expand Down
6 changes: 1 addition & 5 deletions packages/vitest/src/runtime/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@ export async function startVitestExecutor(options: ContextExecutorOptions) {
const state = (): WorkerGlobalState => globalThis.__vitest_worker__ || options.state
const rpc = () => state().rpc

const processExit = process.exit

process.exit = (code = process.exitCode || 0): never => {
const error = new Error(`process.exit called with "${code}"`)
rpc().onWorkerExit(error, code)
return processExit(code)
throw new Error(`process.exit unexpectedly called with "${code}"`)
}

function catchError(err: unknown, type: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function run(ctx: WorkerContext) {
onCancel: setCancel,
},
{
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onWorkerExit'],
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected'],
post(v) { port.postMessage(v) },
on(fn) { port.addListener('message', fn) },
},
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function init(ctx: WorkerContext) {
onCancel: setCancel,
},
{
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onWorkerExit', 'onCancel'],
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onCancel'],
post(v) { port.postMessage(v) },
on(fn) { port.addListener('message', fn) },
},
Expand Down
1 change: 0 additions & 1 deletion packages/vitest/src/types/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export interface RuntimeRPC {
getSourceMap: (id: string, force?: boolean) => Promise<RawSourceMap | undefined>

onFinished: (files: File[], errors?: unknown[]) => void
onWorkerExit: (error: unknown, code?: number) => void
onPathsCollected: (paths: string[]) => void
onUserConsoleLog: (log: UserConsoleLog) => void
onUnhandledError: (err: unknown, type: string) => void
Expand Down

0 comments on commit 8e6c104

Please sign in to comment.