vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Feature: deterministic VITEST_WORKER_ID for each file

Open capaj opened this issue 2 years ago • 2 comments

Clear and concise description of the problem

Would be awesome if we could have a deterministic assignment to VITEST_WORKER_ID. At the moment each spec file runs in a random worker depending on some random events liek for example speed of other spec files before the current one. For example I would like to initialize faker with unique seed per worker, like this:

faker.seed(11+Number(process.env.VITEST_WORKER_ID))

and if it is deterministic I can use snapshots with generated data and they will always match.

Suggested solution

calculate total number of workers(is this possible?), put all the spec files in array, sort them by created date and assign X to each worker. This means that tests created first will always keep the VITEST_WORKER_ID 1, whereas all new tests will get the highest VITEST_WORKER_ID.

Alternative

No response

Additional context

No response

Validations

capaj avatar Mar 01 '23 16:03 capaj

This is how worker ID is generated:

https://github.com/vitest-dev/vitest/blob/819c6cbe40e4b670b5b3d77bc2cc53da4522a3c9/packages/vitest/src/node/pools/threads.ts#L78

sheremet-va avatar Mar 07 '23 18:03 sheremet-va

I think you can just use the file name:

faker.seed(generateHash(expect.getState().testPath))

I am not sure if it works right now outside of a test function, but in theory, it should.

sheremet-va avatar Feb 16 '24 09:02 sheremet-va