Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add maxConcurrency option #1483

Merged
merged 1 commit into from Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.