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

jest-snapshot: Remove report method and throw matcher errors #9049

Merged
merged 7 commits into from Oct 21, 2019

Conversation

pedrottimark
Copy link
Contributor

@pedrottimark pedrottimark commented Oct 13, 2019

Summary

  1. Remove report method from returned object (added in Amend jest snapshots to tie in nicely with Ava #2130 for AVA, but ironically was not called then, nor called now by any dependent of jest-snapshot package)

  2. Throw matcher errors when assertion fails because invalid arguments:

    • Snapshot assertion must not have not
    • Expected properties must be an object
    • Inline snapshot must be a string
    • Received value must be a function
  3. Do not eagerly precompute option because it is not used if an assertion passes

  4. Call new matcherHintFromConfig function after matcher arguments have been validated:

    • Snapshot state must be initialized (error)
    • Received function did not throw (error, but see Residue below)
    • New snapshot was not written (message)
  5. Call renamed printSnapshotName function whenever the count is known:

    • properties did not match (improved)
    • snapshot did not match (baseline)
    • New snapshot was not written (improved)
  6. Improve type annotation of propertiesOrHint and propertiesOrSnapshot Question: do y’all recommend something better than object type?

  7. Make third argument optional in matcherErrorMessage from jest-matcher-utils

Rename print.ts as printSnapshot.ts

Residue

  • Does toThrowErrorMatchingInlineSnapshot need to call stripAddedIndentation?
  • When received function did not throw, call fail instead of throw error?

To minimize breaking changes in Jest 25, wait until next version:

  • Matcher error: Snapshot hint must be a string
  • Add || hint to fullTestName and move it to utils.ts

Test plan

Rename printDiffOrStringified.test.* as printSnapshot.test.*

  • Update 30 snapshot names after enclose existing tests in describe block
  • Add 19 tests to see results and increase code coverage of index.ts file
e2e test change
toMatchSnapshot update 1 test
toThrowErrorMatchingInlineSnapshot update 1 test
toThrowErrorMatchingSnapshot update 1 test

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

@pedrottimark
Copy link
Contributor Author

pedrottimark commented Oct 13, 2019

Matcher error if incorrect modified or invalid arguments:

1 not cannot

2a properties object

2b properties null

3 inline snapshot

I included the following more improved alternative in this pull request

What do you think about the following alternative with snapshot in normal black:

  • because the snapshot is invalid (edge case if someone mangled the inline assertion)
  • and to distinguish the adjacent arguments in the hint

3b inline snapshot noColor

Baseline incorrect behavior is to pass with error snapshot: whatever is not a function

5 received function

Baseline above and improved below:

4a multi-line

4b one-line

But not if valid arguments:

6 snapshot state

Residue: if it is correct to call fail method, then report could include snapshot name:

7 not throw

@codecov-io
Copy link

codecov-io commented Oct 13, 2019

Codecov Report

Merging #9049 into master will increase coverage by 0.6%.
The diff coverage is 95.89%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #9049     +/-   ##
=========================================
+ Coverage   64.05%   64.65%   +0.6%     
=========================================
  Files         277      277             
  Lines       11676    11688     +12     
  Branches     2863     2867      +4     
=========================================
+ Hits         7479     7557     +78     
+ Misses       3572     3514     -58     
+ Partials      625      617      -8
Impacted Files Coverage Δ
packages/jest-snapshot/src/printSnapshot.ts 100% <100%> (ø)
packages/jest-matcher-utils/src/index.ts 82.69% <50%> (-0.54%) ⬇️
packages/jest-snapshot/src/index.ts 71.73% <96.15%> (+44.34%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1d4bd5f...65e20bb. Read the comment docs.

@pedrottimark
Copy link
Contributor Author

pedrottimark commented Oct 17, 2019

I included the more improved version in this pull request as prerequisite for the next pull request to improve colors of snapshot changes: color convention of properties will differ from snapshot

Does color difference help you see which of adjacent arguments in hint is relevant to rest of report?

Current improved is above and potentially more improved is below

When snapshot is not updatable how about properties in green and snapshot in black?

8 properties snapshot

When snapshot is updatable how about properties in black and snapshot in green?

9 properties snapshot

Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

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

I hope you didn't hide anything in the huge rename diffs ;)
Do we have anyone more familiar with jest-snapshot to review its significantly changed impl?

e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts Outdated Show resolved Hide resolved
@pedrottimark
Copy link
Contributor Author

pedrottimark commented Oct 17, 2019

Tim, you have my apology about the rename mess. Sadly, it is up to us to become familiar.

Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

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

Regarding your question on propertiesOr* - toMatchObject also just takes object, that's the same situation, isn't it? Don't think there is a better typing

CHANGELOG.md Outdated Show resolved Hide resolved
@jeysal
Copy link
Contributor

jeysal commented Oct 20, 2019

Sorry that no one has enough time at the moment :|
Code looks good and the mostly unchanged passing e2e also gives me confidence.
Awesome that you're unifying the feel of snapshots and other matchers!

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants