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

Expose toMatchSnapshot in custom matchers #6144

Closed
mattphillips opened this issue May 6, 2018 · 3 comments
Closed

Expose toMatchSnapshot in custom matchers #6144

mattphillips opened this issue May 6, 2018 · 3 comments

Comments

@mattphillips
Copy link
Contributor

mattphillips commented May 6, 2018

🚀 Feature Proposal

Expose toMatchSnapshot in custom matchers

Motivation

Snapshots can seem to vary in outcome across operating systems (see #6102 and #6113). To help prevent this we could normalise the differences in the snapshot.

Currently to normalise a snapshot you would have to do it in the data being snapshotted. By exposing toMatchSnapshot in custom matchers we can create reusable assertions .

This issue is related to #6115 and @rickhanlonii's comment on how to normalise whitespace in snapshots from within a custom matcher.

Example

expect.extend({
  toMatchSnapshotTrimmed: function(received) {
    return this.toMatchSnapshot(received.trim());
  }
})
@donysukardi
Copy link

donysukardi commented May 8, 2018

The same outcome could currently be achieved using the following

// jest-setup.js

const snapshot = require('jest-snapshot');

function toMatchSnapshotTrimmed(received, customName){
  return snapshot.toMatchSnapshot.call(this, received.trim(), customName);
}

expect.extend({
  toMatchSnapshotTrimmed
})

Courtesy of https://github.com/jest-community/snapshot-diff/blob/2f987d90e4a5393192bd1c2c5cef358de9bd78d5/src/index.js#L94

By the way, you can't use arrow function for custom matcher, this context will be lost

@rickhanlonii rickhanlonii added this to the Jest 23 milestone May 21, 2018
@thymikee
Copy link
Collaborator

The idea to easily extend snapshot matchers is pretty neat. But, to do it this way, we would need to couple expect with jest-snapshot in some way (e.g. as a peer dependency) which we don't want to, because it ends up as a browser compatible package.

Since we can achieve the same pretty easily in user space, I think we're good leaving the scope of the expect package as is.

I'd be really happy to merge a PR explaining how to create a custom snapshot matcher in our Expect docs 🙂

@thymikee thymikee removed this from the Jest 23 milestone May 24, 2018
thymikee pushed a commit that referenced this issue Aug 14, 2018
## Summary

Add docs for how to write custom matchers that use snapshot testing, following up from this: #6144 (comment)
@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.
Projects
None yet
Development

No branches or pull requests

5 participants