Skip to content

Commit 66751c9

Browse files
authored
fix(expect): remove JestExtendError.context from verbose error reporting (#9983)
1 parent e92f287 commit 66751c9

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

packages/browser/src/client/tester/runner.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ export function createBrowserRunner(
156156
}
157157

158158
onTaskFinished = async (task: Task) => {
159-
const lastErrorContext = task.result?.errors?.at(-1)?.context
159+
// check custom matcher metadata in JestExtendError
160+
const lastErrorContext = task.result?.errors?.at(-1)?.__vitest_error_context__
160161
if (
161162
this.config.browser.screenshotFailures
162163
&& document.body.clientHeight > 0

packages/expect/src/jest-extend.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,18 @@ function getMatcherState(
6464
}
6565
}
6666

67+
interface VitestErrorContext {
68+
assertionName: string
69+
meta?: object
70+
}
71+
6772
class JestExtendError extends Error {
6873
constructor(
6974
message: string,
7075
public actual?: any,
7176
public expected?: any,
72-
public context?: { assertionName: string; meta?: object },
77+
/** @internal */
78+
public __vitest_error_context__?: VitestErrorContext,
7379
) {
7480
super(message)
7581
}

packages/vitest/src/node/printError.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ const skipErrorProperties = new Set([
283283
'VITEST_TEST_NAME',
284284
'VITEST_TEST_PATH',
285285
'__vitest_rollup_error__',
286+
'__vitest_error_context__',
286287
...Object.getOwnPropertyNames(Error.prototype),
287288
...Object.getOwnPropertyNames(Object.prototype),
288289
])
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { expect, test } from 'vitest'
2+
import { runInlineTests } from '../../test-utils'
3+
4+
// JestExtendError's internal context property shouldn't show up as "Serialized Error"
5+
test('expect.extend error message', async () => {
6+
const result = await runInlineTests({
7+
'./basic.test.js': `
8+
import { expect, test } from 'vitest';
9+
10+
expect.extend({
11+
toMyEqual(actual, expected) {
12+
const pass = actual === expected;
13+
return { pass, message: () => 'my matcher failed', actual, expected };
14+
},
15+
});
16+
17+
test('fail', () => {
18+
expect(123).toMyEqual(456);
19+
});
20+
`,
21+
}, {
22+
reporters: 'verbose',
23+
})
24+
expect(result.stderr).toMatchInlineSnapshot(`
25+
"
26+
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
27+
28+
FAIL basic.test.js > fail
29+
Error: my matcher failed
30+
31+
- Expected
32+
+ Received
33+
34+
- 456
35+
+ 123
36+
37+
❯ basic.test.js:12:15
38+
10|
39+
11| test('fail', () => {
40+
12| expect(123).toMyEqual(456);
41+
| ^
42+
13| });
43+
14|
44+
45+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
46+
47+
"
48+
`)
49+
expect(result.testTree()).toMatchInlineSnapshot(`
50+
{
51+
"basic.test.js": {
52+
"fail": "failed",
53+
},
54+
}
55+
`)
56+
})

0 commit comments

Comments
 (0)