Skip to content

Commit

Permalink
feat: support configure maxTheads and minThreads (#59)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
dominikg and antfu authored Dec 11, 2021
1 parent 07a8f42 commit 67105b4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ $ npx vitest
- Create `vitest.config.ts`, which will have the higher priority
- Pass `--config` option to CLI, e.g. `vitest --config ./path/to/vitest.config.ts`
- Use `process.env.VITEST` to conditionally apply differnet configuration in `vite.config.ts`

- Use `process.env.VITEST_MAX_THREADS` to limit amount of worker threads
To configure `vitest` itself, add `test` property in your Vite config

```ts
Expand Down
12 changes: 8 additions & 4 deletions src/node/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ export async function initViteServer(options: CliOptions = {}) {

resolved.interpretDefault = resolved.interpretDefault || true

const env = process.env
const CI = !!env.CI
const UPDATE_SNAPSHOT = resolved.update || env.UPDATE_SNAPSHOT

const CI = !!process.env.CI
const UPDATE_SNAPSHOT = resolved.update || process.env.UPDATE_SNAPSHOT
resolved.snapshotOptions = {
updateSnapshot: CI && !UPDATE_SNAPSHOT
? 'none'
Expand All @@ -66,6 +64,12 @@ export async function initViteServer(options: CliOptions = {}) {
: 'new',
} as SnapshotStateOptions

if (process.env.VITEST_MAX_THREADS)
resolved.maxThreads = parseInt(process.env.VITEST_MAX_THREADS)

if (process.env.VITEST_MIN_THREADS)
resolved.minThreads = parseInt(process.env.VITEST_MIN_THREADS)

return {
server,
config: resolved,
Expand Down
2 changes: 2 additions & 0 deletions src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface WorkerPool {
export function createWorkerPool(ctx: VitestContext) {
const piscina = new Piscina({
filename: new URL('./dist/node/worker.js', pathToFileURL(distDir)).href,
maxThreads: ctx.config.maxThreads,
minThreads: ctx.config.minThreads,
})

const runTestFiles: WorkerPool['runTestFiles'] = async(files, invalidates) => {
Expand Down
21 changes: 21 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ export interface UserOptions {
reporter?: Reporter

/**
* Enable multi-threading
*
* @default true
*/
threads?: boolean

/**
* Maximum number of threads
*
* @default available CPUs
*/
maxThreads?: number

/**
* Minimum number of threads
*
* @default available CPUs
*/
minThreads?: number

/*
* Interpret CJS module's default as named exports
*
* @default true
Expand Down

0 comments on commit 67105b4

Please sign in to comment.