Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vitest 3.x reference skill covering configuration, test/describe APIs, mocking, coverage, snapshots, and concurrency.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/features-concurrency.md
1---2name: concurrency-parallelism3description: Concurrent tests, parallel execution, and sharding4---56# Concurrency & Parallelism78## File Parallelism910By default, Vitest runs test files in parallel across workers:1112```ts13defineConfig({14test: {15// Run files in parallel (default: true)16fileParallelism: true,1718// Number of worker threads19maxWorkers: 4,20minWorkers: 1,2122// Pool type: 'threads', 'forks', 'vmThreads'23pool: 'threads',24},25})26```2728## Concurrent Tests2930Run tests within a file in parallel:3132```ts33// Individual concurrent tests34test.concurrent('test 1', async ({ expect }) => {35expect(await fetch1()).toBe('result')36})3738test.concurrent('test 2', async ({ expect }) => {39expect(await fetch2()).toBe('result')40})4142// All tests in suite concurrent43describe.concurrent('parallel suite', () => {44test('test 1', async ({ expect }) => {})45test('test 2', async ({ expect }) => {})46})47```4849**Important:** Use `{ expect }` from context for concurrent tests.5051## Sequential in Concurrent Context5253Force sequential execution:5455```ts56describe.concurrent('mostly parallel', () => {57test('parallel 1', async () => {})58test('parallel 2', async () => {})5960test.sequential('must run alone 1', async () => {})61test.sequential('must run alone 2', async () => {})62})6364// Or entire suite65describe.sequential('sequential suite', () => {66test('first', () => {})67test('second', () => {})68})69```7071## Max Concurrency7273Limit concurrent tests:7475```ts76defineConfig({77test: {78maxConcurrency: 5, // Max concurrent tests per file79},80})81```8283## Isolation8485Each file runs in isolated environment by default:8687```ts88defineConfig({89test: {90// Disable isolation for faster runs (less safe)91isolate: false,92},93})94```9596## Sharding9798Split tests across machines:99100```bash101# Machine 1102vitest run --shard=1/3103104# Machine 2105vitest run --shard=2/3106107# Machine 3108vitest run --shard=3/3109```110111### CI Example (GitHub Actions)112113```yaml114jobs:115test:116strategy:117matrix:118shard: [1, 2, 3]119steps:120- run: vitest run --shard=${{ matrix.shard }}/3 --reporter=blob121122merge:123needs: test124steps:125- run: vitest --merge-reports --reporter=junit126```127128### Merge Reports129130```bash131# Each shard outputs blob132vitest run --shard=1/3 --reporter=blob --coverage133vitest run --shard=2/3 --reporter=blob --coverage134135# Merge all blobs136vitest --merge-reports --reporter=json --coverage137```138139## Test Sequence140141Control test order:142143```ts144defineConfig({145test: {146sequence: {147// Run tests in random order148shuffle: true,149150// Seed for reproducible shuffle151seed: 12345,152153// Hook execution order154hooks: 'stack', // 'stack', 'list', 'parallel'155156// All tests concurrent by default157concurrent: true,158},159},160})161```162163## Shuffle Tests164165Randomize to catch hidden dependencies:166167```ts168// Via CLI169vitest --sequence.shuffle170171// Per suite172describe.shuffle('random order', () => {173test('test 1', () => {})174test('test 2', () => {})175test('test 3', () => {})176})177```178179## Pool Options180181### Threads (Default)182183```ts184defineConfig({185test: {186pool: 'threads',187poolOptions: {188threads: {189maxThreads: 8,190minThreads: 2,191isolate: true,192},193},194},195})196```197198### Forks199200Better isolation, slower:201202```ts203defineConfig({204test: {205pool: 'forks',206poolOptions: {207forks: {208maxForks: 4,209isolate: true,210},211},212},213})214```215216### VM Threads217218Full VM isolation per file:219220```ts221defineConfig({222test: {223pool: 'vmThreads',224},225})226```227228## Bail on Failure229230Stop after first failure:231232```bash233vitest --bail 1 # Stop after 1 failure234vitest --bail # Stop on first failure (same as --bail 1)235```236237## Key Points238239- Files run in parallel by default240- Use `.concurrent` for parallel tests within file241- Always use context's `expect` in concurrent tests242- Sharding splits tests across CI machines243- Use `--merge-reports` to combine sharded results244- Shuffle tests to find hidden dependencies245246<!--247Source references:248- https://vitest.dev/guide/features.html#running-tests-concurrently249- https://vitest.dev/guide/improving-performance.html250-->251