Skip to content

Commit

Permalink
BREAKING CHANGE: always run tests in worker (#1390)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 29, 2022
1 parent f36a270 commit 64bee41
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 51 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -90,8 +90,8 @@ jobs:
- name: Test
run: pnpm run test:ci

# - name: Test Single Thread
# run: pnpm run test:ci:no-threads
- name: Test Single Thread
run: pnpm run test:ci:single-thread

test-ui:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
"test:run": "vitest run -r test/core",
"test:all": "cross-env CI=true pnpm -r --stream run test --allowOnly",
"test:ci": "cross-env CI=true pnpm -r --stream --filter !test-fails run test --allowOnly",
"test:ci:no-threads": "cross-env CI=true pnpm -r --stream --filter !test-fails run test --allowOnly --no-threads",
"test:ci:single-thread": "cross-env CI=true pnpm -r --stream --filter !test-fails run test --allowOnly --no-threads",
"typecheck": "tsc --noEmit",
"ui:build": "vite build packages/ui",
"ui:dev": "vite packages/ui",
Expand Down
84 changes: 36 additions & 48 deletions packages/vitest/src/node/pool.ts
Expand Up @@ -18,47 +18,9 @@ export interface WorkerPool {
close: () => Promise<void>
}

export function createPool(ctx: Vitest): WorkerPool {
if (ctx.config.threads)
return createWorkerPool(ctx)
else
return createFakePool(ctx)
}

const workerPath = pathToFileURL(resolve(distDir, './worker.js')).href

export function createFakePool(ctx: Vitest): WorkerPool {
const runWithFiles = (name: 'run' | 'collect'): RunWithFiles => {
return async (files, invalidates) => {
const worker = await import(workerPath)

const { workerPort, port } = createChannel(ctx)

const data: WorkerContext = {
port: workerPort,
config: ctx.getSerializableConfig(),
files,
invalidates,
id: 1,
}

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

return {
runTests: runWithFiles('run'),
close: async () => {},
}
}

export function createWorkerPool(ctx: Vitest): WorkerPool {
export function createPool(ctx: Vitest): WorkerPool {
const threadsCount = ctx.config.watch
? Math.max(cpus().length / 2, 1)
: Math.max(cpus().length - 1, 1)
Expand All @@ -78,35 +40,61 @@ export function createWorkerPool(ctx: Vitest): WorkerPool {
options.concurrentTasksPerWorker = 1
}

if (!ctx.config.threads) {
options.concurrentTasksPerWorker = 1
options.maxThreads = 1
options.minThreads = 1
}

const pool = new Tinypool(options)

const runWithFiles = (name: string): RunWithFiles => {
return async (files, invalidates) => {
let id = 0
const config = ctx.getSerializableConfig()
const results = await Promise.allSettled(files.map(async (file) => {
const { workerPort, port } = createChannel(ctx)

if (!ctx.config.threads) {
const { workerPort, port } = createChannel(ctx)
const data: WorkerContext = {
port: workerPort,
config,
files: [file],
files,
invalidates,
id: ++id,
}

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.')
}
else {
const results = await Promise.allSettled(files.map(async (file) => {
const { workerPort, port } = createChannel(ctx)

const data: WorkerContext = {
port: workerPort,
config,
files: [file],
invalidates,
id: ++id,
}

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

0 comments on commit 64bee41

Please sign in to comment.