vitest-dev / vitest-dev/vitest
Automatic expect tracking for concurrent tests
Nobody has claimed this yet.
- 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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