> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Environment variables

Rstest sets a small set of environment variables you can read from tests, setup files, and source code.

## `NODE_ENV`

Set to `'test'` when the test runner starts (if not already defined). Existing values are preserved, so you can override it from your shell or `package.json` script.

Available as both `process.env.NODE_ENV` and `import.meta.env.NODE_ENV` across all test environments, including [browser mode](/guide/browser-testing/index.md). Prefer `import.meta.env.NODE_ENV` in ESM source code where `process` may not be available.

```ts
if (import.meta.env.NODE_ENV === 'test') {
  // running under Rstest
}
```

## `RSTEST`

Available as both `process.env.RSTEST` and `import.meta.env.RSTEST` across all test environments, including [browser mode](/guide/browser-testing/index.md). The value is set to `'true'` whenever the test runner is active, so you can use it to gate test-only branches in source code.

In browser mode, Rstest replaces these expressions at build time but does not add a global `process` object. Prefer `import.meta.env.RSTEST` in browser-compatible ESM source code, and do not guard access with `typeof process !== 'undefined'` because that condition remains `false` in the browser.

```ts
if (import.meta.env.RSTEST === 'true') {
  // running under Rstest
}
```

For production builds, define the expression used by your source code as `false` so the bundler can eliminate the dead branch — see [Detect Rstest environment](/guide/basic/configure-rstest.md#detect-rstest-environment).

For [in-source tests](/config/test/include-source.md), prefer `import.meta.rstest` — it exposes the Rstest test API directly and is `undefined` in production builds.

## `RSTEST_WORKER_ID`

A stringified integer that uniquely identifies the worker process running the current test file. The first worker is `'1'`. Equivalent to Jest's [`JEST_WORKER_ID`](https://jestjs.io/docs/environment-variables#jest_worker_id).

Use it to isolate shared external resources across parallel workers — typically database schemas, ports, temp directories, or any resource that would collide if two workers used the same name.

```ts title="db.test.ts"
import { afterAll, beforeAll, test } from '@rstest/core';

const dbName = `myapp_test_${process.env.RSTEST_WORKER_ID}`;

beforeAll(async () => {
  await createDatabase(dbName);
});

afterAll(async () => {
  await dropDatabase(dbName);
});

test('inserts a row', async () => {
  // ...
});
```

Concurrent workers are always given different IDs, so the value is safe to use as part of a resource name during a worker's lifetime. The maximum number of concurrent workers is bounded by [`pool.maxWorkers`](/config/test/pool.md); IDs are assigned monotonically as workers spawn and are not recycled within a single Rstest run.
