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

expect/jest-matcher-utils: Improve report when assertion fails, part 5 #7557

Merged
merged 14 commits into from Jan 11, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -34,6 +34,7 @@
- `[jest-validate]` Add syntax to validate multiple permitted types ([#7207](https://github.com/facebook/jest/pull/7207))
- `[jest-config]` Accept an array as as well as a string for `testRegex`([#7209]https://github.com/facebook/jest/pull/7209))
- `[expect/jest-matcher-utils]` Improve report when assertion fails, part 4 ([#7241](https://github.com/facebook/jest/pull/7241))
- `[expect/jest-matcher-utils]` Improve report when assertion fails, part 5 ([#7557](https://github.com/facebook/jest/pull/7557))
- `[expect]` Check constructor equality in .toStrictEqual() ([#7005](https://github.com/facebook/jest/pull/7005))
- `[jest-util]` Add `jest.getTimerCount()` to get the count of scheduled fake timers ([#7285](https://github.com/facebook/jest/pull/7285))
- `[jest-config]` Add `dependencyExtractor` option to use a custom module to extract dependencies from files ([#7313](https://github.com/facebook/jest/pull/7313), [#7349](https://github.com/facebook/jest/pull/7349), [#7350](https://github.com/facebook/jest/pull/7350))
Expand Down
37 changes: 24 additions & 13 deletions docs/ExpectAPI.md
Expand Up @@ -117,7 +117,15 @@ These helper functions and properties can be found on `this` inside a custom mat

#### `this.isNot`

A boolean to let you know this matcher was called with the negated `.not` modifier allowing you to flip your assertion.
A boolean to let you know this matcher was called with the negated `.not` modifier allowing you to flip your assertion and display a clear and correct matcher hint (see example code).

#### `this.promise`

A string allowing you to display a clear and correct matcher hint:

- `'rejects'` if matcher was called with the promise `.rejects` modifier
- `'resolves'` if matcher was called with the promise `.resolves` modifier
- `''` if matcher was not called with a promise modifier

#### `this.equals(a, b)`

Expand All @@ -137,28 +145,31 @@ The most useful ones are `matcherHint`, `printExpected` and `printReceived` to f
const diff = require('jest-diff');
expect.extend({
toBe(received, expected) {
const options = {
comment: 'Object.is equality',
isNot: this.isNot,
promise: this.promise,
};

const pass = Object.is(received, expected);

const message = pass
? () =>
this.utils.matcherHint('.not.toBe') +
this.utils.matcherHint('toBe', undefined, undefined, options) +
'\n\n' +
`Expected value to not be (using Object.is):\n` +
` ${this.utils.printExpected(expected)}\n` +
`Received:\n` +
` ${this.utils.printReceived(received)}`
`Expected: ${this.utils.printExpected(expected)}\n` +
`Received: ${this.utils.printReceived(received)}`
: () => {
const diffString = diff(expected, received, {
const difference = diff(expected, received, {
expand: this.expand,
});
return (
this.utils.matcherHint('.toBe') +
this.utils.matcherHint('toBe', undefined, undefined, options) +
'\n\n' +
`Expected value to be (using Object.is):\n` +
` ${this.utils.printExpected(expected)}\n` +
`Received:\n` +
` ${this.utils.printReceived(received)}` +
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
(difference && difference.includes('- Expect')
? `Difference:\n\n${diffString}`
: `Expected: ${this.utils.printExpected(expected)}\n` +
`Received: ${this.utils.printReceived(received)}`)
);
};

Expand Down
8 changes: 4 additions & 4 deletions e2e/__tests__/__snapshots__/failures.test.js.snap
Expand Up @@ -360,8 +360,8 @@ exports[`works with async failures 1`] = `

expect(received).rejects.toEqual()

Expected received Promise to reject, instead it resolved to value
{\\"foo\\": \\"bar\\"}
Received promise resolved instead of rejected
Resolved to value: {\\"foo\\": \\"bar\\"}

16 |
17 | test('expect reject', () =>
Expand All @@ -377,8 +377,8 @@ exports[`works with async failures 1`] = `

expect(received).resolves.toEqual()

Expected received Promise to resolve, instead it rejected to value
{\\"foo\\": \\"bar\\"}
Received promise rejected instead of resolved
Rejected to value: {\\"foo\\": \\"bar\\"}

19 |
20 | test('expect resolve', () =>
Expand Down
@@ -1,9 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.hasAssertions() throws if expected is not undefined 1`] = `
"<dim>expect(</><red>received</><dim>)[.not].hasAssertions(</><dim>)</>
"<dim>expect(</><red>received</><dim>)[.not].hasAssertions()</>

<bold>Matcher error</>: <green>expected</> value must be omitted or undefined
<bold>Matcher error</>: this matcher must not have an expected argument

Expected has type: number
Expected has value: <green>2</>"
Expand Down