Skip to content

Commit 01daa7d

Browse files
authored
feat: support get runtime config via rs.getConfig API (#701)
1 parent da29d6c commit 01daa7d

File tree

7 files changed

+73
-16
lines changed

7 files changed

+73
-16
lines changed

e2e/test-api/fixtures/setConfig.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ import { describe, expect, it, rs } from '@rstest/core';
22
import { sleep } from '../../scripts';
33

44
rs.setConfig({
5-
testTimeout: 50,
5+
testTimeout: 100,
66
});
77

88
describe('level A', () => {
99
it('it in level A', async () => {
10-
console.log('aaaa');
11-
// await sleep(100);
1210
expect(1 + 1).toBe(3);
1311
});
1412
});
1513

1614
it('it in level B', async () => {
17-
await sleep(100);
15+
await sleep(150);
1816
expect(1 + 1).toBe(2);
1917
});
18+
19+
it('it in level C', async () => {
20+
const config = rs.getConfig();
21+
22+
expect(config.testTimeout).toBe(100);
23+
});

e2e/test-api/setConfig.test.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { describe, expect, it } from '@rstest/core';
1+
import { describe, it } from '@rstest/core';
22
import { runRstestCli } from '../scripts';
33

4-
describe('setConfig', () => {
4+
describe('setConfig & getConfig', () => {
55
it('should throw timeout error when test timeout', async () => {
6-
const { cli } = await runRstestCli({
6+
const { expectExecFailed, expectLog } = await runRstestCli({
77
command: 'rstest',
88
args: ['run', 'fixtures/setConfig.test'],
99
options: {
@@ -13,13 +13,8 @@ describe('setConfig', () => {
1313
},
1414
});
1515

16-
await cli.exec;
17-
expect(cli.exec.process?.exitCode).toBe(1);
18-
const logs = cli.stdout.split('\n').filter(Boolean);
19-
20-
expect(
21-
logs.find((log) => log.includes('Error: test timed out in 50ms')),
22-
).toBeTruthy();
23-
expect(logs.find((log) => log.includes('Tests 2 failed'))).toBeTruthy();
16+
await expectExecFailed();
17+
expectLog(/Error: test timed out in 100ms/);
18+
expectLog(/Tests 2 failed | 1 passed/);
2419
});
2520
});

examples/node/test/index.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { describe, expect, it } from '@rstest/core';
1+
import { describe, expect, it, rs } from '@rstest/core';
22
import { sayHi } from '../src/index';
33

4+
const config = rs.getConfig();
5+
console.log(config);
46
describe('Index', () => {
57
it('should add two numbers correctly', () => {
68
expect(1 + 1).toBe(2);

packages/core/src/runtime/api/utilities.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ export const createRstestUtilities: (
9292
Object.assign(workerState.runtimeConfig, config);
9393
},
9494

95+
getConfig: () => {
96+
const {
97+
testTimeout,
98+
hookTimeout,
99+
clearMocks,
100+
resetMocks,
101+
restoreMocks,
102+
maxConcurrency,
103+
retry,
104+
} = workerState.runtimeConfig;
105+
return {
106+
testTimeout,
107+
hookTimeout,
108+
clearMocks,
109+
resetMocks,
110+
restoreMocks,
111+
maxConcurrency,
112+
retry,
113+
};
114+
},
115+
95116
resetConfig: () => {
96117
if (originalConfig) {
97118
Object.assign(workerState.runtimeConfig, originalConfig);

packages/core/src/types/mock.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ export interface RstestUtilities {
301301
*/
302302
setConfig: (config: RuntimeOptions) => void;
303303

304+
/**
305+
* get runtime config for the current test.
306+
*/
307+
getConfig: () => RuntimeOptions;
308+
304309
/**
305310
* Reset runtime config that were changed with `rstest.setConfig`.
306311
*/

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,18 @@ rstest.resetConfig(); // Restore to default config
124124
- **Type:** `() => void`
125125

126126
Resets the runtime configuration that was changed using `rstest.setConfig` back to the default values.
127+
128+
## rstest.getConfig
129+
130+
- **Alias:** `rs.getConfig`
131+
132+
- **Type:** `() => RuntimeConfig`
133+
134+
Retrieves the current runtime configuration for the test file. Useful for inspecting or logging the current settings.
135+
136+
**Example:**
137+
138+
```ts
139+
const config = rstest.getConfig();
140+
console.log(config);
141+
```

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,18 @@ rstest.resetConfig(); // 恢复默认配置
124124
- **类型:** `() => void`
125125

126126
将通过 `rstest.setConfig` 修改的运行时配置重置为默认值。
127+
128+
## rstest.getConfig
129+
130+
- **别名:** `rs.getConfig`
131+
132+
- **类型:** `() => RuntimeConfig`
133+
134+
获取当前测试文件的运行时配置。
135+
136+
**示例:**
137+
138+
```ts
139+
const config = rstest.getConfig();
140+
console.log(config);
141+
```

0 commit comments

Comments
 (0)