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

Snapshot testing #6115

Closed
mattphillips opened this issue May 2, 2018 · 5 comments
Closed

Snapshot testing #6115

mattphillips opened this issue May 2, 2018 · 5 comments

Comments

@mattphillips
Copy link
Contributor

Question/Feature request.

Hey I'm noticing issues with Snapshots ran on differing operating systems (see #6102 and #6113 as examples).

Is it possible to specify the percentage a snapshot should match on? For example match 90% of the snapshot, allowing for 10% error rate which we don't care about.

If not, and this is something people don't want to add, perhaps a new matcher should be created to deal with whitespace, tabs and newlines etc.

test('ignores whitespace etc', () => {
  expect(`\t hello world\n

                    foo
bar
baz
`).toMatchSnapshotIgnoringXXX();
});
↓↓↓ snapshot ↓↓↓
exports[`ignores whitespace etc 1`] = `
"	 hello world


                      foo
  bar
  baz
  "
`;

This snapshot could be stored with the whitespace etc to keep it human readable but when compared to the value both could be normalised.

@rickhanlonii
Copy link
Member

@mattphillips would this be solved by #5847?

With that you could do:

test('ignores whitespace etc', () => {
  expect(`\t hello world\n

                    foo
bar
baz
`).toMatchSnapshot(
  expect.stringMatching(/hello world/),
);

Another option we've discussed - exposing toMatchSnapshot to custom matchers so you could do something like:

expect.extend({
  toMatchSnapshotTrimmed: (received) => {
    return this.toMatchSnapshot(received.trim());
  }
})

@mattphillips
Copy link
Contributor Author

@rickhanlonii #5847 looks very cool and would certainly help with this! Whats happening with the proposal is it currently being worked on?

I think exposing toMatchSnapshot to custom matchers would be slick too, then we could create matchers like toMatchSnapshotTrimmed in jest-extended is any work happening on this too?

@SimenB
Copy link
Member

SimenB commented May 6, 2018

@mattphillips mind changing this issue (or opening up a new one) to be about exposing toMatchSnapshot to custom matchers?

@mattphillips
Copy link
Contributor Author

Closed in favour of #6144

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

No branches or pull requests

3 participants