Skip to content

Commit

Permalink
expect/jest-matcher-utils: Improve report when assertion fails, part 5 (
Browse files Browse the repository at this point in the history
#7557)

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

* Edit utils and improve one snapshot

* Update CHANGELOG.md

* Improve message for ensureNoExpected

* Rewrite promise instead message

* Rewrite half of non-promise value tests with not

* Delete empty line and duplicated words

* Rebuild and update e2e snapshot

* Add section about promise property to ExpectAPI.md

* Update example code to use options in ExpectAPI.md

* Added promise property to MatcherState
  • Loading branch information
pedrottimark authored and thymikee committed Jan 11, 2019
1 parent 60ca308 commit f425bd4
Show file tree
Hide file tree
Showing 14 changed files with 485 additions and 329 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,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 @@ -365,8 +365,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
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit f425bd4

Please sign in to comment.