Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(expect): resolve message on assertion errors #10989

Merged
merged 1 commit into from
Dec 30, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- `[babel-plugin-jest-hoist]` Add `__dirname` and `__filename` to whitelisted globals ([#10903](https://github.com/facebook/jest/pull/10903))
- `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708))
- `[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))
- `[expect]` [**BREAKING**] `matcherResult` on `JestAssertionError` are now strings rather than functions ([#10989] (https://github.com/facebook/jest/pull/10989))
- `[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)
- `[jest-circus]` Fix `testLocation` on Windows when using `test.each` ([#10871](https://github.com/facebook/jest/pull/10871))
- `[jest-cli]` Use testFailureExitCode when bailing from a failed test ([#10958](https://github.com/facebook/jest/pull/10958))
Expand Down
40 changes: 40 additions & 0 deletions e2e/__tests__/failureDetailsProperty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ test('that the failureDetails property is set', () => {
"matcherResult": Object {
"actual": true,
"expected": false,
"message": "expect(received).toBe(expected) // Object.is equality

Expected: false
Received: true",
"name": "toBe",
"pass": false,
},
Expand All @@ -59,6 +63,10 @@ test('that the failureDetails property is set', () => {
"matcherResult": Object {
"actual": true,
"expected": false,
"message": "expect(received).toBe(expected) // Object.is equality

Expected: false
Received: true",
"name": "toBe",
"pass": false,
},
Expand Down Expand Up @@ -90,6 +98,18 @@ test('that the failureDetails property is set', () => {
\\"p1\\": \\"hello\\",
\\"p2\\": \\"sunshine\\",
}",
"message": "expect(received).toMatchInlineSnapshot(snapshot)

Snapshot name: \`my test a snapshot failure 1\`

- Snapshot - 1
+ Received + 1

Object {
\\"p1\\": \\"hello\\",
- \\"p2\\": \\"sunshine\\",
+ \\"p2\\": \\"world\\",
}",
"name": "toMatchInlineSnapshot",
"pass": false,
},
Expand Down Expand Up @@ -169,6 +189,10 @@ test('that the failureDetails property is set', () => {
"matcherResult": Object {
"actual": true,
"expected": false,
"message": "expect(received).toBe(expected) // Object.is equality

Expected: false
Received: true",
"name": "toBe",
"pass": false,
},
Expand All @@ -179,6 +203,10 @@ test('that the failureDetails property is set', () => {
"matcherResult": Object {
"actual": true,
"expected": false,
"message": "expect(received).toBe(expected) // Object.is equality

Expected: false
Received: true",
"name": "toBe",
"pass": false,
},
Expand All @@ -195,6 +223,18 @@ test('that the failureDetails property is set', () => {
\\"p1\\": \\"hello\\",
\\"p2\\": \\"sunshine\\",
}",
"message": "expect(received).toMatchInlineSnapshot(snapshot)

Snapshot name: \`my test a snapshot failure 1\`

- Snapshot - 1
+ Received + 1

Object {
\\"p1\\": \\"hello\\",
- \\"p2\\": \\"sunshine\\",
+ \\"p2\\": \\"world\\",
}",
"name": "toMatchInlineSnapshot",
"pass": false,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/expect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import type {
import {iterableEquality, subsetEquality} from './utils';

class JestAssertionError extends Error {
matcherResult?: SyncExpectationResult;
matcherResult?: Omit<SyncExpectationResult, 'message'> & {message: string};
}

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

Choose a reason for hiding this comment

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

message is resolved to a string on line 275


if (throws) {
throw error;
Expand Down