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: wait for console.log to print a message before terminating a worker #2861

Merged
merged 1 commit into from
Feb 13, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/vitest/src/runtime/rpc.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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()
}