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: Improve report when matcher fails, part 15 #8281

Merged
merged 8 commits into from Apr 8, 2019

Conversation

Projects
None yet
5 participants
@pedrottimark
Copy link
Collaborator

commented Apr 6, 2019

Summary

For toBe matcher:

  • Display matcher name in regular black instead of dim color
  • Display promise rejects or resolves in matcher hint
  • Omit redundant or irrelevant information, as described below

When negative assertion fails:

  • Display not between expected label and value
  • Omit received value because it is redundant

When positive assertion fails:

  • Display either diff (without Difference label) or Expected/Received values, not both
  • Display either Difference or message about deep equality independent of the above , not both
  • Display Received value has no visual difference in black instead of dim
  • Display If the test should pass with deep equality, replace toBe with ${deepEqualityName} to emphasize toStrictEqual more than toEqual matcher

@jeysal your work on shouldPrintDiff in #7605 gave me important example for the logic :)

Residue: pretty-format serializes [, 1] and [undefined, 1] the same (it would be a breaking change to fix for sparse arrays, but it is an example for improvement via data-driven diff)

Test plan

  • Updated 18 snapshot tests
  • Added 7 9 snapshot tests

Friendly reviewers, you have my apology for mess of some added and updated snapshots :(

See also pictures in following comments with baseline at left and improved at right

@pedrottimark pedrottimark changed the title expect: Improve report when matcher fail, part 15 expect: Improve report when matcher fails, part 15 Apr 6, 2019

@pedrottimark

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 6, 2019

Received value is redundant in negative assertion of referential identity:

true string

Display difference if it is relevant:

false truthy object unequal

false truthy string 4-3

@pedrottimark

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 6, 2019

Display message about no visual difference without irrelevant Difference label:

false falsey no-visual function

false falsey no-visual symbol

@pedrottimark

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 6, 2019

See #8281 (comment) instead of this comment

Your critique and improvement of the proposed message is welcome:

  • If the testit should pass with deep equality, replace toBe with toStrictEqual
  • If the testit should pass with deep equality, replace toBe with toEqual

Rewrite message not to mislead for expected failures during first phase of TDD and to emphasize toStrictEqual more than toEqual matcher, sorry about ruthless cropping at left edge of pictures:

false falsey no-visual object toStrictEqual

false falsey no-visual object toEqual

If referential identity test fails and deep equality test would pass, is it okay to omit the diff?

false falsey object toStrictEqual

false falsey object toEqual

@pedrottimark

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 6, 2019

Display primitive as Expected/Received values:

false falsey boolean

false falsey number

false falsey named symbol

Different types:

false falsey primitive types

false falsey structure types

At least one string is not multiline:

false falsey string 1-1

false falsey string 2-1

Specific other types:

false falsey error

false falsey regexp

false falsey named function

@jeysal

jeysal approved these changes Apr 7, 2019

Copy link
Collaborator

left a comment

This output is a lot smarter, thanks @pedrottimark.
I love the part about trying toStrictEqual and then toEqual to suggest what's likely the better fit.

Show resolved Hide resolved packages/expect/src/matchers.ts Outdated
(received instanceof Error && expected instanceof Error);

const deepEqualityName =
isShallow || (expectedType !== 'object' && expectedType !== 'array')

This comment has been minimized.

Copy link
@jeysal

jeysal Apr 7, 2019

Collaborator

Can you explain why the checks on this line are necessary and why deepEqualityName affects difference a few lines below? I'm probably missing something, I don't quite understand why the logic needs to be this complex.

This comment has been minimized.

Copy link
@pedrottimark

pedrottimark Apr 7, 2019

Author Collaborator

Renamed isShallowInequality and refactored assignments to deepEqualityName

Your intuition was correct that difference was too complex.

I moved the message below hint and above difference or Expected/Received lines.

This comment has been minimized.

Copy link
@jeysal

jeysal Apr 7, 2019

Collaborator

Cool, now the deepEqualityName and difference concerns are nicely separated. I've just tried it locally and it looks like you can also remove the deepEqualityName conditions? At least it doesn't seem to fail any test.

-          if (!isShallowInequality) {
-            if (expectedType === 'object' || expectedType === 'array') {

This comment has been minimized.

Copy link
@pedrottimark

pedrottimark Apr 7, 2019

Author Collaborator

Yes indeed, thank you for patient critique!

  • For deepEqualityName the only if condition is skip map and set until we review the current questionable logic
  • Separate and rename hasConciseReport to make its purpose clear to short-circuit a line diff (while we improve the remaining matchers, we will discover whether it is reusable)
  • Added condition and 2 snapshot tests for deep equal and unequal dates
@pedrottimark

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 7, 2019

Updated pictures supersede #8281 (comment) and illustrate #8281 (comment)

false falsey object toStrictEqual 2

false falsey object toEqual 2

false falsey no-visual object toStrictEqual 2

false falsey no-visual object toEqual 2

@jeysal

jeysal approved these changes Apr 7, 2019

@thymikee
Copy link
Collaborator

left a comment

👍

Show resolved Hide resolved packages/expect/src/matchers.ts Outdated

thymikee and others added some commits Apr 8, 2019

Update packages/expect/src/matchers.ts
Co-Authored-By: pedrottimark <pedrottimark@gmail.com>

@pedrottimark pedrottimark merged commit 2fc468c into facebook:master Apr 8, 2019

10 of 11 checks passed

ci/circleci: test-jest-circus Your tests failed on CircleCI
Details
ci/circleci: lint-and-typecheck Your tests passed on CircleCI!
Details
ci/circleci: test-browser Your tests passed on CircleCI!
Details
ci/circleci: test-node-10 Your tests passed on CircleCI!
Details
ci/circleci: test-node-11 Your tests passed on CircleCI!
Details
ci/circleci: test-node-6 Your tests passed on CircleCI!
Details
ci/circleci: test-node-8 Your tests passed on CircleCI!
Details
ci/circleci: test-or-deploy-website Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
facebook.jest #20190408.3 succeeded
Details

@pedrottimark pedrottimark deleted the pedrottimark:improve-report-15 branch Apr 8, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.