vitest-dev / vitest-dev/vitest

Automatic expect tracking for concurrent tests

Open
#5,665 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

p2-nice-to-have
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Automatic expect tracking for concurrent tests

It's cumbersome and error prone to accept expect for each concurrent test
Automatic tracking is also allows to make tests faster by using .concurrent wrapper, without code modifications

import { describe, it } from 'vitest'

// All tests within this suite will be run in parallel
describe.concurrent('suite', () => {
  it('concurrent test 1', async ({ expect }) => { /* ... */ })
  it('concurrent test 2', async ({ expect }) => { /* ... */ })
  it.concurrent('concurrent test 3', async ({ expect }) => { /* ... */ })
})
Suggested solution

We can add opt in option to use AsyncLocalStorage for node.

import { AsyncLocalStorage } from 'node:async_hooks';

const expectStorage = new AsyncLocalStorage();

const getTestExpect = () => expectStorage.getStore()

const expect = (...args: Parameters<typeof originalExpect>) => {
  const localExpect = getTestExpect()
  if (localExpect) {
    return localExpect(...args)
  }
  return originalExpect(...args)
}
const it = (testFunction: Parameters<typeof originalIt>) => {
    originalIt((...args) => {
      const {expect} = args[0]
      
      expectStorage(expect, testFunction, ...args)
    })
}
Alternative

Zone.js can be also used for async context tracking, since it monkeypatches a lot of stuff - it less reliable

Additional context

No response

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Vitest's existing expect handling and concurrent test execution, then evaluate the proposed opt-in AsyncLocalStorage approach for Node.js. Done should provide automatic per-test expect tracking for concurrent suites without requiring each test to accept expect, with coverage for the described concurrent cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.