Skip to content

Commit 95c7ccb

Browse files
authored
feat: stubEnv support import.meta.env (#594)
1 parent fb6dae2 commit 95c7ccb

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

e2e/spy/stubEnv.test.mts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect, it, rstest } from '@rstest/core';
2+
3+
it('test stubEnv & unstubAllEnvs in import.meta.env', () => {
4+
const env = import.meta.env.NODE_ENV;
5+
const mockEnv = env === 'production' ? 'development' : 'production';
6+
rstest.stubEnv('NODE_ENV', mockEnv);
7+
8+
rstest.stubEnv('TEST_111', '111');
9+
10+
expect(import.meta.env.NODE_ENV).toBe(mockEnv);
11+
12+
expect(import.meta.env.TEST_111).toBe('111');
13+
14+
rstest.stubEnv('NODE_ENV', undefined);
15+
expect(import.meta.env.NODE_ENV).toBeUndefined();
16+
17+
rstest.unstubAllEnvs();
18+
19+
expect(import.meta.env.NODE_ENV).toBe(env);
20+
expect(import.meta.env.TEST_111).toBeUndefined();
21+
});

packages/core/src/core/plugins/basic.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const pluginBasic: (context: RstestContext) => RsbuildPlugin = (
3838
source: {
3939
define: {
4040
'import.meta.rstest': "global['@rstest/core']",
41+
'import.meta.env': 'process.env',
4142
},
4243
},
4344
output: {

packages/core/tests/core/__snapshots__/rsbuild.test.ts.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (jsdom) 1`] =
428428
DefinePlugin {
429429
"_args": [
430430
{
431+
"import.meta.env": "process.env",
431432
"import.meta.env.ASSET_PREFIX": """",
432433
"import.meta.env.BASE_URL": ""/"",
433434
"import.meta.env.DEV": false,
@@ -940,6 +941,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (node) 1`] = `
940941
DefinePlugin {
941942
"_args": [
942943
{
944+
"import.meta.env": "process.env",
943945
"import.meta.env.ASSET_PREFIX": """",
944946
"import.meta.env.BASE_URL": ""/"",
945947
"import.meta.env.DEV": false,
@@ -1454,6 +1456,7 @@ exports[`prepareRsbuild > should generate rspack config correctly in watch mode
14541456
DefinePlugin {
14551457
"_args": [
14561458
{
1459+
"import.meta.env": "process.env",
14571460
"import.meta.env.ASSET_PREFIX": """",
14581461
"import.meta.env.BASE_URL": ""/"",
14591462
"import.meta.env.DEV": false,
@@ -1986,6 +1989,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
19861989
DefinePlugin {
19871990
"_args": [
19881991
{
1992+
"import.meta.env": "process.env",
19891993
"import.meta.env.ASSET_PREFIX": """",
19901994
"import.meta.env.BASE_URL": ""/"",
19911995
"import.meta.env.DEV": false,
@@ -2509,6 +2513,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
25092513
DefinePlugin {
25102514
"_args": [
25112515
{
2516+
"import.meta.env": "process.env",
25122517
"import.meta.env.ASSET_PREFIX": """",
25132518
"import.meta.env.BASE_URL": ""/"",
25142519
"import.meta.env.DEV": false,

website/docs/en/api/rstest/utilities.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ A set of useful utility functions.
88

99
- **Type:** `(name: string, value: string | undefined) => Rstest`
1010

11-
Temporarily sets an environment variable in `process.env` to the specified value. Useful for testing code that depends on environment variables.
11+
Temporarily sets an environment variable in `process.env` and `import.meta.env` to the specified value. Useful for testing code that depends on environment variables.
1212

13-
- If `value` is `undefined`, the variable will be removed from `process.env`.
13+
- If `value` is `undefined`, the variable will be removed from `process.env` and `import.meta.env`.
1414
- You can call this multiple times to stub multiple variables.
1515
- Use `rstest.unstubAllEnvs()` to restore all environment variables changed by this method.
1616

@@ -19,9 +19,11 @@ Temporarily sets an environment variable in `process.env` to the specified value
1919
```ts
2020
rstest.stubEnv('NODE_ENV', 'test');
2121
expect(process.env.NODE_ENV).toBe('test');
22+
expect(import.meta.env.NODE_ENV).toBe('test');
2223

2324
rstest.stubEnv('MY_VAR', undefined);
2425
expect(process.env.MY_VAR).toBeUndefined();
26+
expect(import.meta.env.MY_VAR).toBeUndefined();
2527
```
2628

2729
## rstest.unstubAllEnvs

website/docs/zh/api/rstest/utilities.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
- **类型:** `(name: string, value: string | undefined) => Rstest`
1010

11-
临时设置 `process.env` 中的环境变量为指定值。适用于测试依赖环境变量的代码。
11+
临时设置 `process.env` `import.meta.env` 中的环境变量为指定值。适用于测试依赖环境变量的代码。
1212

13-
- 如果 `value``undefined`,该变量会从 `process.env` 中移除。
13+
- 如果 `value``undefined`,该变量会从 `process.env` `import.meta.env` 中移除。
1414
- 可多次调用以模拟多个环境变量。
1515
- 使用 `rstest.unstubAllEnvs()` 可恢复所有通过此方法更改的环境变量。
1616

@@ -19,9 +19,11 @@
1919
```ts
2020
rstest.stubEnv('NODE_ENV', 'test');
2121
expect(process.env.NODE_ENV).toBe('test');
22+
expect(import.meta.env.NODE_ENV).toBe('test');
2223

2324
rstest.stubEnv('MY_VAR', undefined);
2425
expect(process.env.MY_VAR).toBeUndefined();
26+
expect(import.meta.env.MY_VAR).toBeUndefined();
2527
```
2628

2729
## rstest.unstubAllEnvs

0 commit comments

Comments
 (0)