Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add VITEST_WORKER_ID env #774

Merged
merged 2 commits into from Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
2 changes: 1 addition & 1 deletion packages/vitest/src/node/cli.ts
Expand Up @@ -76,7 +76,7 @@ async function run(cliFilters: string[], options: CliOptions) {
async function start(cliFilters: string[], options: CliOptions) {
process.env.TEST = 'true'
process.env.VITEST = 'true'
process.env.NODE_ENV = 'test'
process.env.NODE_ENV ??= 'test'

if (options.run)
options.watch = false
Expand Down
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()
})