Skip to content

Commit fbc54c9

Browse files
authoredFeb 13, 2023
fix: wait for console.log to print a message before terminating a worker (#2861)
1 parent 2672c58 commit fbc54c9

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed
 

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,30 @@ function withSafeTimers(fn: () => void) {
2323
}
2424
}
2525

26+
const promises = new Set<Promise<unknown>>()
27+
28+
export const rpcDone = () => {
29+
if (!promises.size)
30+
return
31+
const awaitable = Array.from(promises)
32+
return Promise.all(awaitable)
33+
}
34+
2635
export const rpc = () => {
2736
const { rpc } = getWorkerState()
2837
return new Proxy(rpc, {
2938
get(target, p, handler) {
3039
const sendCall = Reflect.get(target, p, handler)
31-
const safeSendCall = (...args: any[]) => withSafeTimers(() => sendCall(...args))
40+
const safeSendCall = (...args: any[]) => withSafeTimers(async () => {
41+
const result = sendCall(...args)
42+
promises.add(result)
43+
try {
44+
return await result
45+
}
46+
finally {
47+
promises.delete(result)
48+
}
49+
})
3250
safeSendCall.asEvent = sendCall.asEvent
3351
return safeSendCall
3452
},

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { getWorkerState } from '../utils/global'
1111
import type { MockMap } from '../types/mocker'
1212
import type { VitestExecutor } from './execute'
1313
import { createVitestExecutor } from './execute'
14-
import { rpc } from './rpc'
14+
import { rpc, rpcDone } from './rpc'
1515

1616
let _viteNode: {
1717
run: (files: string[], config: ResolvedConfig, executor: VitestExecutor) => Promise<void>
@@ -109,5 +109,6 @@ function init(ctx: WorkerContext) {
109109
export async function run(ctx: WorkerContext) {
110110
init(ctx)
111111
const { run, executor } = await startViteNode(ctx)
112-
return run(ctx.files, ctx.config, executor)
112+
await run(ctx.files, ctx.config, executor)
113+
await rpcDone()
113114
}

0 commit comments

Comments
 (0)
Please sign in to comment.