Skip to content

Commit

Permalink
fix: handle error from pool.run
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed May 21, 2022
1 parent 60b55df commit d28cbf0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
27 changes: 19 additions & 8 deletions packages/vitest/src/node/pool.ts
Expand Up @@ -9,6 +9,7 @@ import type { RawSourceMap } from 'vite-node'
import type { WorkerContext, WorkerRPC } from '../types'
import { distDir } from '../constants'
import type { Vitest } from './core'
import { AggregateError } from '../utils'

export type RunWithFiles = (files: string[], invalidates?: string[]) => Promise<void>

Expand Down Expand Up @@ -41,10 +42,12 @@ 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 +83,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 +94,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)
}
}
}

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

export { resolve as resolvePath }

class AggregateErrorPonyfill extends Error {
errors: unknown[]
constructor(errors: Iterable<unknown>, message = '') {
super(message)
this.errors = [...errors]
}
}

// AggregateError is supported in Node.js 15.0.0+
export const AggregateError = global.AggregateError || AggregateErrorPonyfill

0 comments on commit d28cbf0

Please sign in to comment.