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
15 changes: 15 additions & 0 deletions e2e/test-api/edgeCase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ describe('Test Edge Cases', () => {
expect(logs.find((log) => log.includes('Error: Symbol('))).toBeFalsy();
});

it('should handle error string correctly', async () => {
const { expectExecFailed, expectLog } = await runRstestCli({
command: 'rstest',
args: ['run', 'fixtures/errorString.test.ts'],
options: {
nodeOptions: {
cwd: __dirname,
},
},
});
await expectExecFailed();

expectLog('Unknown Error: aaaa');
});

it('test module not found', async () => {
// Module not found errors should be silent at build time, and throw errors at runtime
const { cli, expectExecSuccess } = await runRstestCli({
Expand Down
7 changes: 7 additions & 0 deletions e2e/test-api/fixtures/errorString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { it } from '@rstest/core';

it('test error string', () => {
return new Promise((_resolve, reject) => {
reject('aaaa');
Copy link

Copilot AI Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The error message 'aaaa' is a magic string that doesn't provide meaningful context. Consider using a more descriptive error message like 'Test error string' to make the test intention clearer.

Suggested change
reject('aaaa');
reject('Test error string');

Copilot uses AI. Check for mistakes.
});
});
4 changes: 3 additions & 1 deletion packages/core/src/runtime/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export const getRealTimers = (): typeof REAL_TIMERS => {
export const formatTestError = (err: any, test?: Test): FormattedError[] => {
const errors = Array.isArray(err) ? err : [err];

return errors.map((error) => {
return errors.map((rawError) => {
const error =
typeof rawError === 'string' ? { message: rawError } : rawError;
const errObj: FormattedError = {
...error,
// Some error attributes cannot be enumerated
Expand Down
12 changes: 12 additions & 0 deletions website/docs/en/guide/migration/jest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ The `done` callback is not supported in Rstest. Instead, you can return a Promis
+ }));
```

If you need to handle errors, you can modify it as follows:

```diff
- test('async test with done', (done) => {
+ test('async test with done', () => new Promise((resolve, reject) => {
+ const done = err => (err ? reject(err) : resolve());
// ...
done(error);
- });
+ }));
```

### Hooks

The return functions of the `beforeEach` and `beforeAll` hooks in Rstest are used to perform cleaning work after testing.
Expand Down
12 changes: 12 additions & 0 deletions website/docs/zh/guide/migration/jest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ Rstest 不支持 `done` 回调。作为替代,你可以返回一个 Promise
+ }));
```

如果你需要处理错误,你可以按照以下方式修改:

```diff
- test('async test with done', (done) => {
+ test('async test with done', () => new Promise((resolve, reject) => {
+ const done = err => (err ? reject(err) : resolve());
// ...
done(error);
- });
+ }));
```

### Hooks

Rstest 中 `beforeEach` 和 `beforeAll` 钩子的返回函数用于执行测试后的清理工作。
Expand Down
Loading