Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vitest): gracefully exit Vitest if process.exit is called inside the test #4903

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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