Skip to content

Commit fa16ea8

Browse files
authored
fix: should handle error string correctly (#614)
1 parent 5dac4d8 commit fa16ea8

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

e2e/test-api/edgeCase.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ describe('Test Edge Cases', () => {
2525
expect(logs.find((log) => log.includes('Error: Symbol('))).toBeFalsy();
2626
});
2727

28+
it('should handle error string correctly', async () => {
29+
const { expectExecFailed, expectLog } = await runRstestCli({
30+
command: 'rstest',
31+
args: ['run', 'fixtures/errorString.test.ts'],
32+
options: {
33+
nodeOptions: {
34+
cwd: __dirname,
35+
},
36+
},
37+
});
38+
await expectExecFailed();
39+
40+
expectLog('Unknown Error: aaaa');
41+
});
42+
2843
it('test module not found', async () => {
2944
// Module not found errors should be silent at build time, and throw errors at runtime
3045
const { cli, expectExecSuccess } = await runRstestCli({
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { it } from '@rstest/core';
2+
3+
it('test error string', () => {
4+
return new Promise((_resolve, reject) => {
5+
reject('aaaa');
6+
});
7+
});

packages/core/src/runtime/util.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export const getRealTimers = (): typeof REAL_TIMERS => {
1919
export const formatTestError = (err: any, test?: Test): FormattedError[] => {
2020
const errors = Array.isArray(err) ? err : [err];
2121

22-
return errors.map((error) => {
22+
return errors.map((rawError) => {
23+
const error =
24+
typeof rawError === 'string' ? { message: rawError } : rawError;
2325
const errObj: FormattedError = {
2426
...error,
2527
// Some error attributes cannot be enumerated

website/docs/en/guide/migration/jest.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ The `done` callback is not supported in Rstest. Instead, you can return a Promis
129129
+ }));
130130
```
131131

132+
If you need to handle errors, you can modify it as follows:
133+
134+
```diff
135+
- test('async test with done', (done) => {
136+
+ test('async test with done', () => new Promise((resolve, reject) => {
137+
+ const done = err => (err ? reject(err) : resolve());
138+
// ...
139+
done(error);
140+
- });
141+
+ }));
142+
```
143+
132144
### Hooks
133145

134146
The return functions of the `beforeEach` and `beforeAll` hooks in Rstest are used to perform cleaning work after testing.

website/docs/zh/guide/migration/jest.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ Rstest 不支持 `done` 回调。作为替代,你可以返回一个 Promise
129129
+ }));
130130
```
131131

132+
如果你需要处理错误,你可以按照以下方式修改:
133+
134+
```diff
135+
- test('async test with done', (done) => {
136+
+ test('async test with done', () => new Promise((resolve, reject) => {
137+
+ const done = err => (err ? reject(err) : resolve());
138+
// ...
139+
done(error);
140+
- });
141+
+ }));
142+
```
143+
132144
### Hooks
133145

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

0 commit comments

Comments
 (0)