diff --git a/docs/config/index.md b/docs/config/index.md index f9b7b0deafe4..a96789e5d019 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -315,7 +315,7 @@ Silent console output from tests Path to setup files. They will be run before each test file. -You can use `process.env.VITEST_WORKER_ID` (integer-like string) inside to distinguish between threads (will always be `'1'`, if run with `threads: false`). +You can use `process.env.VITEST_POOL_ID` (integer-like string) inside to distinguish between threads (will always be `'1'`, if run with `threads: false`). :::tip Note, that if you are running [`--no-threads`](#threads), this file will be run in the same global scope. Meaning, that you are accessing the same global object before each test, so make sure you are not doing the same thing more than you need. diff --git a/docs/guide/migration.md b/docs/guide/migration.md index e13686b79f7e..6006d8c184eb 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -20,7 +20,7 @@ Jest exports various [`jasmine`](https://jasmine.github.io/) globals (such as `j **Envs** -Just like Jest, Vitest sets `NODE_ENV` to `test`, if it wasn't set before. Vitest also has a counterpart for `JEST_WORKER_ID` called `VITEST_WORKER_ID`, so if you rely on it, don't forget to rename it. +Just like Jest, Vitest sets `NODE_ENV` to `test`, if it wasn't set before. Vitest also has a counterpart for `JEST_WORKER_ID` called `VITEST_POOL_ID` (always less than or equal to `maxThreads`), so if you rely on it, don't forget to rename it. Vitest also exposes `VITEST_WORKER_ID` which is a unique ID of a running worker - this number is not affected by `maxThreads`, and will increase with each created worker. **Done Callback** diff --git a/packages/vitest/src/node/pool.ts b/packages/vitest/src/node/pool.ts index 3f170a6bb19d..0843fe9f47a4 100644 --- a/packages/vitest/src/node/pool.ts +++ b/packages/vitest/src/node/pool.ts @@ -25,14 +25,17 @@ export function createPool(ctx: Vitest): WorkerPool { ? Math.max(cpus().length / 2, 1) : Math.max(cpus().length - 1, 1) + const maxThreads = ctx.config.maxThreads ?? threadsCount + const minThreads = ctx.config.minThreads ?? threadsCount + const options: TinypoolOptions = { filename: workerPath, // TODO: investigate further // It seems atomics introduced V8 Fatal Error https://github.com/vitest-dev/vitest/issues/1191 useAtomics: false, - maxThreads: ctx.config.maxThreads ?? threadsCount, - minThreads: ctx.config.minThreads ?? threadsCount, + maxThreads, + minThreads, } if (ctx.config.isolate) { @@ -66,12 +69,14 @@ export function createPool(ctx: Vitest): WorkerPool { async function runFiles(files: string[], invalidates: string[] = []) { const { workerPort, port } = createChannel(ctx) + const workerId = ++id const data: WorkerContext = { port: workerPort, config, files, invalidates, - id: ++id, + workerId, + poolId: !ctx.config.threads ? 1 : ((workerId - 1) % maxThreads) + 1, } try { await pool.run(data, { transferList: [workerPort], name }) diff --git a/packages/vitest/src/runtime/worker.ts b/packages/vitest/src/runtime/worker.ts index 9159535cecf6..d3b0da3d5f20 100644 --- a/packages/vitest/src/runtime/worker.ts +++ b/packages/vitest/src/runtime/worker.ts @@ -63,9 +63,10 @@ function init(ctx: WorkerContext) { if (typeof __vitest_worker__ !== 'undefined' && ctx.config.threads && ctx.config.isolate) throw new Error(`worker for ${ctx.files.join(',')} already initialized by ${getWorkerState().ctx.files.join(',')}. This is probably an internal bug of Vitest.`) - const { config, port, id } = ctx + const { config, port, workerId, poolId } = ctx - process.env.VITEST_WORKER_ID = String(id) + process.env.VITEST_WORKER_ID = String(workerId) + process.env.VITEST_POOL_ID = String(poolId) // @ts-expect-error I know what I am doing :P globalThis.__vitest_worker__ = { diff --git a/packages/vitest/src/types/worker.ts b/packages/vitest/src/types/worker.ts index e48b05a59c25..1ff9e70c0fac 100644 --- a/packages/vitest/src/types/worker.ts +++ b/packages/vitest/src/types/worker.ts @@ -8,7 +8,8 @@ import type { SnapshotResult } from './snapshot' import type { UserConsoleLog } from './general' export interface WorkerContext { - id: number + workerId: number + poolId: number port: MessagePort config: ResolvedConfig files: string[] diff --git a/test/core/test/env.test.ts b/test/core/test/env.test.ts index 29a89aef91fb..67d154c358dd 100644 --- a/test/core/test/env.test.ts +++ b/test/core/test/env.test.ts @@ -36,6 +36,7 @@ test('can see env in "define"', () => { test('has worker env', () => { expect(process.env.VITEST_WORKER_ID).toBeDefined() + expect(process.env.VITEST_POOL_ID).toBeDefined() }) test('custom env', () => {