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: handle error from pool.run #1348

Merged
merged 4 commits into from May 21, 2022
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
28 changes: 20 additions & 8 deletions packages/vitest/src/node/pool.ts
Expand Up @@ -8,6 +8,7 @@ import { createBirpc } from 'birpc'
import type { RawSourceMap } from 'vite-node'
import type { WorkerContext, WorkerRPC } from '../types'
import { distDir } from '../constants'
import { AggregateError } from '../utils'
import type { Vitest } from './core'

export type RunWithFiles = (files: string[], invalidates?: string[]) => Promise<void>
Expand Down Expand Up @@ -41,10 +42,13 @@ export function createFakePool(ctx: Vitest): WorkerPool {
id: 1,
}

await worker[name](data, { transferList: [workerPort] })

port.close()
workerPort.close()
try {
await worker[name](data, { transferList: [workerPort] })
}
finally {
port.close()
workerPort.close()
}
}
}

Expand Down Expand Up @@ -80,7 +84,7 @@ export function createWorkerPool(ctx: Vitest): WorkerPool {
return async (files, invalidates) => {
let id = 0
const config = ctx.getSerializableConfig()
await Promise.all(files.map(async (file) => {
const results = await Promise.allSettled(files.map(async (file) => {
const { workerPort, port } = createChannel(ctx)

const data: WorkerContext = {
Expand All @@ -91,10 +95,18 @@ export function createWorkerPool(ctx: Vitest): WorkerPool {
id: ++id,
}

await pool.run(data, { transferList: [workerPort], name })
port.close()
workerPort.close()
try {
await pool.run(data, { transferList: [workerPort], name })
}
finally {
port.close()
workerPort.close()
}
}))

const errors = results.filter((r): r is PromiseRejectedResult => r.status === 'rejected').map(r => r.reason)
if (errors.length > 0)
throw new AggregateError(errors, 'Errors occurred while running tests. For more information, see serialized error.')
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/vitest/src/utils/index.ts
Expand Up @@ -129,3 +129,13 @@ export function getCallLastIndex(code: string) {
}

export { resolve as resolvePath }

// AggregateError is supported in Node.js 15.0.0+
class AggregateErrorPonyfill extends Error {
errors: unknown[]
constructor(errors: Iterable<unknown>, message = '') {
super(message)
this.errors = [...errors]
}
}
export { AggregateErrorPonyfill as AggregateError }