Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions e2e/test-api/fixtures/setConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import { describe, expect, it, rs } from '@rstest/core';
import { sleep } from '../../scripts';

rs.setConfig({
testTimeout: 50,
testTimeout: 100,
});

describe('level A', () => {
it('it in level A', async () => {
console.log('aaaa');
// await sleep(100);
expect(1 + 1).toBe(3);
});
});

it('it in level B', async () => {
await sleep(100);
await sleep(150);
expect(1 + 1).toBe(2);
});

it('it in level C', async () => {
const config = rs.getConfig();

expect(config.testTimeout).toBe(100);
});
17 changes: 6 additions & 11 deletions e2e/test-api/setConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expect, it } from '@rstest/core';
import { describe, it } from '@rstest/core';
import { runRstestCli } from '../scripts';

describe('setConfig', () => {
describe('setConfig & getConfig', () => {
it('should throw timeout error when test timeout', async () => {
const { cli } = await runRstestCli({
const { expectExecFailed, expectLog } = await runRstestCli({
command: 'rstest',
args: ['run', 'fixtures/setConfig.test'],
options: {
Expand All @@ -13,13 +13,8 @@ describe('setConfig', () => {
},
});

await cli.exec;
expect(cli.exec.process?.exitCode).toBe(1);
const logs = cli.stdout.split('\n').filter(Boolean);

expect(
logs.find((log) => log.includes('Error: test timed out in 50ms')),
).toBeTruthy();
expect(logs.find((log) => log.includes('Tests 2 failed'))).toBeTruthy();
await expectExecFailed();
expectLog(/Error: test timed out in 100ms/);
expectLog(/Tests 2 failed | 1 passed/);
});
});
4 changes: 3 additions & 1 deletion examples/node/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, expect, it } from '@rstest/core';
import { describe, expect, it, rs } from '@rstest/core';
import { sayHi } from '../src/index';

const config = rs.getConfig();
console.log(config);
describe('Index', () => {
it('should add two numbers correctly', () => {
expect(1 + 1).toBe(2);
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/runtime/api/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ export const createRstestUtilities: (
Object.assign(workerState.runtimeConfig, config);
},

getConfig: () => {
const {
testTimeout,
hookTimeout,
clearMocks,
resetMocks,
restoreMocks,
maxConcurrency,
retry,
} = workerState.runtimeConfig;
return {
testTimeout,
hookTimeout,
clearMocks,
resetMocks,
restoreMocks,
maxConcurrency,
retry,
};
},

resetConfig: () => {
if (originalConfig) {
Object.assign(workerState.runtimeConfig, originalConfig);
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ export interface RstestUtilities {
*/
setConfig: (config: RuntimeOptions) => void;

/**
* get runtime config for the current test.
*/
getConfig: () => RuntimeOptions;

/**
* Reset runtime config that were changed with `rstest.setConfig`.
*/
Expand Down
15 changes: 15 additions & 0 deletions website/docs/en/api/rstest/utilities.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,18 @@ rstest.resetConfig(); // Restore to default config
- **Type:** `() => void`

Resets the runtime configuration that was changed using `rstest.setConfig` back to the default values.

## rstest.getConfig

- **Alias:** `rs.getConfig`

- **Type:** `() => RuntimeConfig`

Retrieves the current runtime configuration for the test file. Useful for inspecting or logging the current settings.

**Example:**

```ts
const config = rstest.getConfig();
console.log(config);
```
15 changes: 15 additions & 0 deletions website/docs/zh/api/rstest/utilities.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,18 @@ rstest.resetConfig(); // 恢复默认配置
- **类型:** `() => void`

将通过 `rstest.setConfig` 修改的运行时配置重置为默认值。

## rstest.getConfig

- **别名:** `rs.getConfig`

- **类型:** `() => RuntimeConfig`

获取当前测试文件的运行时配置。

**示例:**

```ts
const config = rstest.getConfig();
console.log(config);
```
Loading