Skip to content

Commit

Permalink
feat: add maxConcurrency option (#1483)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 14, 2022
1 parent 58818f2 commit 348a008
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 3 deletions.
9 changes: 9 additions & 0 deletions docs/config/index.md
Expand Up @@ -533,3 +533,12 @@ Vitest will not fail, if no tests will be found.
- **Default**: `false`

Show heap usage after each test. Useful for debugging memory leaks.

### maxConcurrency

- **Type**: `number`
- **Default**: `5`

A number of tests that are allowed to run at the same time marked with `test.concurrent`.

Test above this limit will be queued to run when available slot appears.
1 change: 1 addition & 0 deletions packages/vitest/package.json
Expand Up @@ -119,6 +119,7 @@
"micromatch": "^4.0.5",
"mlly": "^0.5.2",
"natural-compare": "^1.4.0",
"p-limit": "^4.0.0",
"pathe": "^0.2.0",
"picocolors": "^1.0.0",
"pkg-types": "^0.3.2",
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/defaults.ts
Expand Up @@ -72,6 +72,7 @@ const config = {
open: true,
coverage: coverageConfigDefaults,
fakeTimers: fakeTimersDefaults,
maxConcurrency: 5,
}

export const configDefaults: Required<Pick<UserConfig, keyof typeof config>> = Object.freeze(config)
8 changes: 5 additions & 3 deletions packages/vitest/src/runtime/run.ts
@@ -1,3 +1,4 @@
import limit from 'p-limit'
import type { File, HookCleanupCallback, HookListener, ResolvedConfig, Suite, SuiteHooks, Task, TaskResult, TaskState, Test } from '../types'
import { vi } from '../integrations/vi'
import { getSnapshotClient } from '../integrations/snapshot/chai'
Expand Down Expand Up @@ -195,6 +196,8 @@ export async function runSuite(suite: Suite) {

updateTask(suite)

const workerState = getWorkerState()

if (suite.mode === 'skip') {
suite.result.state = 'skip'
}
Expand All @@ -207,7 +210,8 @@ export async function runSuite(suite: Suite) {

for (const tasksGroup of partitionSuiteChildren(suite)) {
if (tasksGroup[0].concurrent === true) {
await Promise.all(tasksGroup.map(c => runSuiteChild(c)))
const mutex = limit(workerState.config.maxConcurrency)
await Promise.all(tasksGroup.map(c => mutex(() => runSuiteChild(c))))
}
else {
for (const c of tasksGroup)
Expand All @@ -225,8 +229,6 @@ export async function runSuite(suite: Suite) {
}
suite.result.duration = now() - start

const workerState = getWorkerState()

if (workerState.config.logHeapUsage)
suite.result.heap = process.memoryUsage().heapUsed

Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/types/config.ts
Expand Up @@ -344,6 +344,12 @@ export interface InlineConfig {
* Return `false` to ignore the log.
*/
onConsoleLog?: (log: string, type: 'stdout' | 'stderr') => false | void

/**
* A number of tests that are allowed to run at the same time marked with `test.concurrent`.
* @default 5
*/
maxConcurrency?: number
}

export interface UserConfig extends InlineConfig {
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 348a008

Please sign in to comment.