Search code examples
javascripttestingvitevitest

Identify which thread a test is running in in vitest


I would like to use an in-memory database for some integration test suites in vitest. As part of that, I'd like to, in a beforeEach block, restore the database to a specific state. However, I want to make sure that tests that are running in parallel do not conflict with one another. I'd rather not have to create a new database for every test, so it seems like the right thing is to set up a database for each thread that's running. Is there a way, from inside of the vite test to know which thread it is in?


Solution

  • Different threads in vitest can be distinguished by environment variables automatically set by vitest. Use something like this:

    ${process.env.VITEST_POOL_ID}.{process.env.VITEST_WORKER_ID}