Skip to content

Commit

Permalink
fix: wait for console.log to print a message before terminating a wor…
Browse files Browse the repository at this point in the history
…ker (#2861)
  • Loading branch information
sheremet-va committed Feb 13, 2023
1 parent 2672c58 commit fbc54c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
20 changes: 19 additions & 1 deletion packages/vitest/src/runtime/rpc.ts
Expand Up @@ -23,12 +23,30 @@ function withSafeTimers(fn: () => void) {
}
}

const promises = new Set<Promise<unknown>>()

export const rpcDone = () => {
if (!promises.size)
return
const awaitable = Array.from(promises)
return Promise.all(awaitable)
}

export const rpc = () => {
const { rpc } = getWorkerState()
return new Proxy(rpc, {
get(target, p, handler) {
const sendCall = Reflect.get(target, p, handler)
const safeSendCall = (...args: any[]) => withSafeTimers(() => sendCall(...args))
const safeSendCall = (...args: any[]) => withSafeTimers(async () => {
const result = sendCall(...args)
promises.add(result)
try {
return await result
}
finally {
promises.delete(result)
}
})
safeSendCall.asEvent = sendCall.asEvent
return safeSendCall
},
Expand Down
5 changes: 3 additions & 2 deletions packages/vitest/src/runtime/worker.ts
Expand Up @@ -11,7 +11,7 @@ import { getWorkerState } from '../utils/global'
import type { MockMap } from '../types/mocker'
import type { VitestExecutor } from './execute'
import { createVitestExecutor } from './execute'
import { rpc } from './rpc'
import { rpc, rpcDone } from './rpc'

let _viteNode: {
run: (files: string[], config: ResolvedConfig, executor: VitestExecutor) => Promise<void>
Expand Down Expand Up @@ -109,5 +109,6 @@ function init(ctx: WorkerContext) {
export async function run(ctx: WorkerContext) {
init(ctx)
const { run, executor } = await startViteNode(ctx)
return run(ctx.files, ctx.config, executor)
await run(ctx.files, ctx.config, executor)
await rpcDone()
}

0 comments on commit fbc54c9

Please sign in to comment.