Skip to content

Commit c2f152d

Browse files
authored
fix(expect): resolve message on assertion errors (jestjs#10989)
1 parent e3d5491 commit c2f152d

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- `[babel-plugin-jest-hoist]` Add `__dirname` and `__filename` to whitelisted globals ([#10903](https://github.com/facebook/jest/pull/10903))
2626
- `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708))
2727
- `[expect]` [**BREAKING**] Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119) & [#10929](https://github.com/facebook/jest/pull/10929))
28+
- `[expect]` [**BREAKING**] `matcherResult` on `JestAssertionError` are now strings rather than functions ([#10989] (https://github.com/facebook/jest/pull/10989))
2829
- `[jest-circus]` Fixed the issue of beforeAll & afterAll hooks getting executed even if it is inside a skipped `describe` block [#10451](https://github.com/facebook/jest/issues/10451)
2930
- `[jest-circus]` Fix `testLocation` on Windows when using `test.each` ([#10871](https://github.com/facebook/jest/pull/10871))
3031
- `[jest-cli]` Use testFailureExitCode when bailing from a failed test ([#10958](https://github.com/facebook/jest/pull/10958))

e2e/__tests__/failureDetailsProperty.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ test('that the failureDetails property is set', () => {
3434
"matcherResult": Object {
3535
"actual": true,
3636
"expected": false,
37+
"message": "expect(received).toBe(expected) // Object.is equality
38+
39+
Expected: false
40+
Received: true",
3741
"name": "toBe",
3842
"pass": false,
3943
},
@@ -59,6 +63,10 @@ test('that the failureDetails property is set', () => {
5963
"matcherResult": Object {
6064
"actual": true,
6165
"expected": false,
66+
"message": "expect(received).toBe(expected) // Object.is equality
67+
68+
Expected: false
69+
Received: true",
6270
"name": "toBe",
6371
"pass": false,
6472
},
@@ -90,6 +98,18 @@ test('that the failureDetails property is set', () => {
9098
\\"p1\\": \\"hello\\",
9199
\\"p2\\": \\"sunshine\\",
92100
}",
101+
"message": "expect(received).toMatchInlineSnapshot(snapshot)
102+
103+
Snapshot name: \`my test a snapshot failure 1\`
104+
105+
- Snapshot - 1
106+
+ Received + 1
107+
108+
Object {
109+
\\"p1\\": \\"hello\\",
110+
- \\"p2\\": \\"sunshine\\",
111+
+ \\"p2\\": \\"world\\",
112+
}",
93113
"name": "toMatchInlineSnapshot",
94114
"pass": false,
95115
},
@@ -169,6 +189,10 @@ test('that the failureDetails property is set', () => {
169189
"matcherResult": Object {
170190
"actual": true,
171191
"expected": false,
192+
"message": "expect(received).toBe(expected) // Object.is equality
193+
194+
Expected: false
195+
Received: true",
172196
"name": "toBe",
173197
"pass": false,
174198
},
@@ -179,6 +203,10 @@ test('that the failureDetails property is set', () => {
179203
"matcherResult": Object {
180204
"actual": true,
181205
"expected": false,
206+
"message": "expect(received).toBe(expected) // Object.is equality
207+
208+
Expected: false
209+
Received: true",
182210
"name": "toBe",
183211
"pass": false,
184212
},
@@ -195,6 +223,18 @@ test('that the failureDetails property is set', () => {
195223
\\"p1\\": \\"hello\\",
196224
\\"p2\\": \\"sunshine\\",
197225
}",
226+
"message": "expect(received).toMatchInlineSnapshot(snapshot)
227+
228+
Snapshot name: \`my test a snapshot failure 1\`
229+
230+
- Snapshot - 1
231+
+ Received + 1
232+
233+
Object {
234+
\\"p1\\": \\"hello\\",
235+
- \\"p2\\": \\"sunshine\\",
236+
+ \\"p2\\": \\"world\\",
237+
}",
198238
"name": "toMatchInlineSnapshot",
199239
"pass": false,
200240
},

packages/expect/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import type {
5050
import {iterableEquality, subsetEquality} from './utils';
5151

5252
class JestAssertionError extends Error {
53-
matcherResult?: SyncExpectationResult;
53+
matcherResult?: Omit<SyncExpectationResult, 'message'> & {message: string};
5454
}
5555

5656
const isPromise = <T extends any>(obj: any): obj is PromiseLike<T> =>
@@ -293,7 +293,7 @@ const makeThrowingMatcher = (
293293
// Passing the result of the matcher with the error so that a custom
294294
// reporter could access the actual and expected objects of the result
295295
// for example in order to display a custom visual diff
296-
error.matcherResult = result;
296+
error.matcherResult = {...result, message};
297297

298298
if (throws) {
299299
throw error;

0 commit comments

Comments
 (0)