Skip to content

Commit

Permalink
feat: add VITEST_WORKER_ID env
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Feb 17, 2022
1 parent 50b5d6e commit f01c507
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion docs/config/index.md
Expand Up @@ -252,7 +252,9 @@ Silent mode

- **Type:** `string | string[]`

Path to setup files
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`).

### globalSetup

Expand Down
6 changes: 5 additions & 1 deletion docs/guide/migration.md
Expand Up @@ -18,4 +18,8 @@ Unlike Jest, mocked modules in `<root>/__mocks__` are not loaded unless `vi.mock

**Jasmine API**

Jest exports various [`jasmine`](https://jasmine.github.io/) globals (such as `jasmine.any()`). Any such instances will need to be migrated to [their Vitest counterparts](/api/).
Jest exports various [`jasmine`](https://jasmine.github.io/) globals (such as `jasmine.any()`). Any such instances will need to be migrated to [their Vitest counterparts](/api/).

**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.
3 changes: 3 additions & 0 deletions packages/vitest/src/node/pool.ts
Expand Up @@ -39,6 +39,7 @@ export function createFakePool(ctx: Vitest): WorkerPool {
config: ctx.getConfig(),
files,
invalidates,
id: 1,
}

await worker[name](data, { transferList: [workerPort] })
Expand Down Expand Up @@ -78,6 +79,7 @@ export function createWorkerPool(ctx: Vitest): WorkerPool {

const runWithFiles = (name: string): RunWithFiles => {
return async(files, invalidates) => {
let id = 0
await Promise.all(files.map(async(file) => {
const { workerPort, port } = createChannel(ctx)

Expand All @@ -86,6 +88,7 @@ export function createWorkerPool(ctx: Vitest): WorkerPool {
config: ctx.getConfig(),
files: [file],
invalidates,
id: ++id,
}

await pool.run(data, { transferList: [workerPort], name })
Expand Down
4 changes: 3 additions & 1 deletion packages/vitest/src/runtime/worker.ts
Expand Up @@ -58,7 +58,9 @@ function init(ctx: WorkerContext) {

process.stdout.write('\0')

const { config, port } = ctx
const { config, port, id } = ctx

process.env.VITEST_WORKER_ID = String(id)

// @ts-expect-error I know what I am doing :P
globalThis.__vitest_worker__ = {
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/types/worker.ts
Expand Up @@ -7,6 +7,7 @@ import type { SnapshotResult } from './snapshot'
import type { UserConsoleLog } from './general'

export interface WorkerContext {
id: number
port: MessagePort
config: ResolvedConfig
files: string[]
Expand Down
4 changes: 4 additions & 0 deletions test/core/test/env.test.ts
Expand Up @@ -17,3 +17,7 @@ test('can see env in "define"', () => {
expect(import.meta.env.TEST_NAME).toBe('hello world')
expect(process.env.TEST_NAME).toBe('hello world')
})

test('has worker env', () => {
expect(process.env.VITEST_WORKER_ID).toBeDefined()
})

0 comments on commit f01c507

Please sign in to comment.