Skip to content

Commit

Permalink
fix(expect): resolve message on assertion errors (#10989)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Dec 30, 2020
1 parent e3d5491 commit c2f152d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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};

if (throws) {
throw error;
Expand Down

0 comments on commit c2f152d

Please sign in to comment.