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

Proposal: toMatchSnapshot property matchers #5847

Closed
rickhanlonii opened this issue Mar 21, 2018 · 1 comment · Fixed by #6210
Closed

Proposal: toMatchSnapshot property matchers #5847

rickhanlonii opened this issue Mar 21, 2018 · 1 comment · Fixed by #6210

Comments

@rickhanlonii
Copy link
Member

Motivation

Objects often contain variable data. Unfortunately, the current snapshot API requires "cleaning" these values out of the object before passing to toMatchSnapshot. This proposal is to give users the ability to specify the shape of the object in the snapshot.

Proposal

I propose that toMatchSnapshot accepts a new argument which is deeply merged into the received object before snapshotting. If the value of the merge object field is an asymmetric matcher, the matcher is verified and then the name of the matcher is used in the snapshot.

Example

const user = {
  id: 'user-id',
  rev: 3, // increments on save
  createdAt: 1521639165339,
};

test('user', () => {
  expect(user).toMatchSnapshot({
    rev: 1,
    createdAt: expect.any(Date), 
  })
});

// snapshot
exports[`user 1`] = `
Object {
  "createdAt": '[expect.any(Date)]',
  "id": "user-id",
  "rev": "1",
}
`;

Why core

This could be solved with a custom matcher. I think this should be in core because:

  • It neatly ties together snapshots and asymmetric matchers (main reason)
  • Improves experience for API snapshot testing (many APIs return dates), inviting non-web devs
  • Core currently has all of the control for snapshots and does not expose it to custom matchers
@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

Successfully merging a pull request may close this issue.

1 participant