Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions e2e/env/fixtures/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, it } from '@rstest/core';

it('should get environment variables correctly', () => {
expect(process.env.printLogger).toBe('true');
});
7 changes: 7 additions & 0 deletions e2e/env/fixtures/rstest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from '@rstest/core';

export default defineConfig({
env: {
printLogger: 'true',
},
});
25 changes: 25 additions & 0 deletions e2e/env/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, it } from '@rstest/core';
import { join } from 'pathe';
import { runRstestCli } from '../scripts/';

const __filename = fileURLToPath(import.meta.url);

const __dirname = dirname(__filename);

describe('test environment variables', () => {
it('should get environment variables correctly in test', async () => {
const { expectExecSuccess } = await runRstestCli({
command: 'rstest',
args: ['run', 'index.test.ts'],
options: {
nodeOptions: {
cwd: join(__dirname, 'fixtures'),
},
},
});

await expectExecSuccess();
});
});
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ const createDefaultConfig = (): NormalizedConfig => ({
printConsoleTrace: false,
disableConsoleIntercept: false,
snapshotFormat: {},
env: {},
coverage: {
exclude: [
'**/node_modules/**',
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ const getRuntimeConfig = (context: ProjectContext): RuntimeConfig => {
isolate,
coverage,
snapshotFormat,
env,
} = context.normalizedConfig;

return {
env,
testNamePattern,
testTimeout,
hookTimeout,
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/runtime/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ const registerGlobalApi = (api: Rstest) => {
const listeners: (() => void)[] = [];
let isTeardown = false;

const setupEnv = (env?: Partial<NodeJS.ProcessEnv>) => {
if (env) {
Object.assign(process.env, env);
}
};

const preparePool = async ({
entryInfo: { distPath, testPath },
sourceMaps,
Expand All @@ -47,9 +53,12 @@ const preparePool = async ({
disableConsoleIntercept,
testEnvironment,
snapshotFormat,
env,
},
} = context;

setupEnv(env);

if (!disableConsoleIntercept) {
const { createCustomConsole } = await import('./console');

Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ export interface RstestConfig {
/** Format snapshot output */
snapshotFormat?: SnapshotFormat;

/**
* Custom environment variables available on `process.env` during tests.
*/
env?: Partial<NodeJS.ProcessEnv>;

/**
* Coverage options
*/
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type RuntimeConfig = Pick<
| 'hookTimeout'
| 'coverage'
| 'snapshotFormat'
| 'env'
>;

export type WorkerContext = {
Expand Down
1 change: 1 addition & 0 deletions website/docs/en/config/test/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"includeSource",
"testNamePattern",

"env",
"retry",
"testTimeout",
"hookTimeout",
Expand Down
30 changes: 30 additions & 0 deletions website/docs/en/config/test/env.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
overviewHeaders: []
---

# env

- **Type:** `Partial<NodeJS.ProcessEnv>`
- **Default:** `undefined`

Custom environment variables available on `process.env` during tests.

```ts title="rstest.config.ts"
import { defineConfig } from '@rstest/core';

export default defineConfig({
env: {
PRINT_LOGGER: 'true',
},
});
```

After setting the above configuration, you can access the `PRINT_LOGGER` variable in your tests:

```ts title="index.test.ts"
import { describe, it } from '@rstest/core';

if (process.env.PRINT_LOGGER === 'true') {
console.log('PRINT_LOGGER is enabled');
}
```
1 change: 1 addition & 0 deletions website/docs/zh/config/test/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"includeSource",
"testNamePattern",

"env",
"retry",
"testTimeout",
"hookTimeout",
Expand Down
30 changes: 30 additions & 0 deletions website/docs/zh/config/test/env.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
overviewHeaders: []
---

# env

- **类型:** `Partial<NodeJS.ProcessEnv>`
- **默认值:** `undefined`

自定义环境变量,在测试过程中可以通过 `process.env` 访问。

```ts title="rstest.config.ts"
import { defineConfig } from '@rstest/core';

export default defineConfig({
env: {
PRINT_LOGGER: 'true',
},
});
```

在设置上述配置后,现在,你可以在测试中访问 `PRINT_LOGGER` 变量:

```ts title="index.test.ts"
import { describe, it } from '@rstest/core';

if (process.env.PRINT_LOGGER === 'true') {
console.log('PRINT_LOGGER is enabled');
}
```
2 changes: 1 addition & 1 deletion website/theme/components/ConfigOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const OVERVIEW_GROUPS: Group[] = [
},
{
name: 'runtime',
items: ['retry', 'testTimeout', 'hookTimeout', 'maxConcurrency'],
items: ['env', 'retry', 'testTimeout', 'hookTimeout', 'maxConcurrency'],
},
{
name: 'environment',
Expand Down
Loading