vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.94k stars 1.16k forks source link

Read thread ID inside a test #4848

Closed danilofuchs closed 9 months ago

danilofuchs commented 9 months ago

Clear and concise description of the problem

Our tests rely on a shared database connection, with --no-isolate and --no-threads as they may have side effects.

To speed it up, we want to run them in parallel, setting up multiple databases, one for each vitest thread.

Suggested solution

A field inside ctx.task that identifies the thread ID, so we can choose a different database.

Alternative

Run tests with --shard and use an environment variable for each shard.

Additional context

No response

Validations

sheremet-va commented 9 months ago

There are already two variables:

https://vitest.dev/guide/migration.html#envs

  1. VITEST_WORKER_ID is incremented for every new worker spawned
  2. VITEST_POOL_ID is the number of worker (>= minWorkers && <=maxWorkers)