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

Feature proposal: Take snapshots of comparisons #3888

Closed
ferrannp opened this issue Jun 23, 2017 · 9 comments
Closed

Feature proposal: Take snapshots of comparisons #3888

ferrannp opened this issue Jun 23, 2017 · 9 comments

Comments

@ferrannp
Copy link
Contributor

Do you want to request a feature or report a bug?

Feature.

What is the current behavior?

One use case is for a component with a lot of conditions depending on props. We end up
with a lot of huge and repetitive snapshots.

What is the expected behavior?

This was brought up in Twitter recently but I think this would be awesome:

screen shot 2017-06-23 at 17 31 29

This is what I was actually missing when writing my Jest snapshot articles, when for example for this one:

// RepoItem-Snapshot-test.snap (huge snapshot)
...
style={
  Array [
    Object {
      "backgroundColor": "transparent"
    },
    undefined
  ]
}
...

vs

// RepoItem-Snapshot-test.snap (huge snapshot)
...
style={
  Array [
    Object {
      "backgroundColor": "transparent"
    },
    Object {
      "backgroundColor": "#B2DFDB"
    }
  ]
}
...

I am only interested on that part and not in the whole snapshot again. So basically this method will generate a small snapshot containing only the differences between the 2 full snapshots.

@kentcdodds
Copy link
Contributor

This would make snapshots reviewable. Right now they're very easy to make too big to be reviewable.

@tricoder42
Copy link

tricoder42 commented Jun 23, 2017

EDIT: Digging in twitter thread, I've found that @thejameskyle already did this: https://twitter.com/thejameskyle/status/874330782923243520

What about passing a diff to the toMatchSnapshot matcher. Something like:

expect(diff(<Component size="small" />, <Component size="small" />)).toMatchSnapshot()

I tried to calculate diff between snapshots using jest-diff... and then let jest to compare these diffs:

import React from 'react'
import diff from 'jest-diff'

it('', function() {
    expect(diff(<span>Hello World</span>, <span className='test'>Hello World</span>)).toMatchSnapshot()
})

snimek obrazovky 2017-06-23 v 21 43 06

Obviously, it needs to remove colors and maybe tweak the first diff, but it highlights the changes between snapshots.

What do you think?

@kentcdodds
Copy link
Contributor

Sounds about good. Also, we'd want the same serializers applied.

@cpojer
Copy link
Member

cpojer commented Jun 27, 2017

There are already (long!) issues tracking similar things: #2202 and #2197. I agree this needs to happen, but it needs somebody to champion the feature. @kentcdodds or @ferrannp are you up for it?

@cpojer cpojer closed this as completed Jun 27, 2017
@kentcdodds
Copy link
Contributor

Makes sense. Thanks @cpojer! It's unlikely I'll have any time to work on this anytime soon.

@thymikee
Copy link
Collaborator

@kentcdodds
Copy link
Contributor

Thank you so much! This is so great! 🎉

Would love to see this built-in 😉

@cpojer
Copy link
Member

cpojer commented Jul 28, 2017

Why were you keeping this from me? :D

@github-actions
Copy link

This issue 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 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants