Skip to content

Commit

Permalink
feat: introduce VITEST_POOL_ID (#1473)
Browse files Browse the repository at this point in the history
* feat: introduce VITEST_POOL_ID

* fix: always increment worker id, pool id is always 1 in `--no-threads`
  • Loading branch information
sheremet-va committed Jun 13, 2022
1 parent 1012534 commit 0b639b1
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/config/index.md
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/migration.md
Expand Up @@ -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**

Expand Down
11 changes: 8 additions & 3 deletions packages/vitest/src/node/pool.ts
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 })
Expand Down
5 changes: 3 additions & 2 deletions packages/vitest/src/runtime/worker.ts
Expand Up @@ -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__ = {
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/types/worker.ts
Expand Up @@ -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[]
Expand Down
1 change: 1 addition & 0 deletions test/core/test/env.test.ts
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 0b639b1

Please sign in to comment.