Skip to content

Commit

Permalink
fix: handle error from pool.run (#1348)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed May 21, 2022
1 parent 478465a commit fbaa546
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
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 }

0 comments on commit fbaa546

Please sign in to comment.