Skip to content

Commit 8e6c104

Browse files
authoredJan 9, 2024
fix(vitest): gracefully exit Vitest if process.exit is called inside the test (#4903)
1 parent 043b78f commit 8e6c104

File tree

7 files changed

+5
-15
lines changed

7 files changed

+5
-15
lines changed
 

‎packages/vitest/src/node/error.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ export async function printError(error: unknown, project: WorkspaceProject | und
5959
const nearest = error instanceof TypeCheckError
6060
? error.stacks[0]
6161
: stacks.find(stack =>
62-
project.getModuleById(stack.file)
63-
&& existsSync(stack.file),
62+
project.server && project.getModuleById(stack.file) && existsSync(stack.file),
6463
)
6564

6665
const errorProperties = getErrorProperties(e)

‎packages/vitest/src/node/pools/rpc.ts

-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import type { WorkspaceProject } from '../workspace'
55
export function createMethodsRPC(project: WorkspaceProject): RuntimeRPC {
66
const ctx = project.ctx
77
return {
8-
async onWorkerExit(error, code) {
9-
await ctx.logger.printError(error, { type: 'Unexpected Exit', fullStack: true })
10-
process.exit(code || 1)
11-
},
128
snapshotSaved(snapshot) {
139
ctx.snapshot.add(snapshot)
1410
},

‎packages/vitest/src/runtime/child.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async function init(ctx: ChildContext) {
3434
onCancel: setCancel,
3535
},
3636
{
37-
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onWorkerExit', 'onCancel'],
37+
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onCancel'],
3838
serialize: v8.serialize,
3939
deserialize: v => v8.deserialize(Buffer.from(v)),
4040
post(v) {

‎packages/vitest/src/runtime/execute.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,8 @@ export async function startVitestExecutor(options: ContextExecutorOptions) {
7070
const state = (): WorkerGlobalState => globalThis.__vitest_worker__ || options.state
7171
const rpc = () => state().rpc
7272

73-
const processExit = process.exit
74-
7573
process.exit = (code = process.exitCode || 0): never => {
76-
const error = new Error(`process.exit called with "${code}"`)
77-
rpc().onWorkerExit(error, code)
78-
return processExit(code)
74+
throw new Error(`process.exit unexpectedly called with "${code}"`)
7975
}
8076

8177
function catchError(err: unknown, type: string) {

‎packages/vitest/src/runtime/vm.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function run(ctx: WorkerContext) {
3131
onCancel: setCancel,
3232
},
3333
{
34-
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onWorkerExit'],
34+
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected'],
3535
post(v) { port.postMessage(v) },
3636
on(fn) { port.addListener('message', fn) },
3737
},

‎packages/vitest/src/runtime/worker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async function init(ctx: WorkerContext) {
3232
onCancel: setCancel,
3333
},
3434
{
35-
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onWorkerExit', 'onCancel'],
35+
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onCancel'],
3636
post(v) { port.postMessage(v) },
3737
on(fn) { port.addListener('message', fn) },
3838
},

‎packages/vitest/src/types/rpc.ts

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export interface RuntimeRPC {
1515
getSourceMap: (id: string, force?: boolean) => Promise<RawSourceMap | undefined>
1616

1717
onFinished: (files: File[], errors?: unknown[]) => void
18-
onWorkerExit: (error: unknown, code?: number) => void
1918
onPathsCollected: (paths: string[]) => void
2019
onUserConsoleLog: (log: UserConsoleLog) => void
2120
onUnhandledError: (err: unknown, type: string) => void

0 commit comments

Comments
 (0)
Please sign in to comment.