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: toMatchSnapshot should let the caller know if the comparison was a match #5802

Closed
jasonnet opened this issue Mar 15, 2018 · 5 comments

Comments

@jasonnet
Copy link

Feature

Current behavior

Right now toMatchSnapshot calls dontThrow. This prevents the call to toMatchSnapshot from throwing an exception even if the match fails. This precludes the caller from detecting if the snapshot match failed.

Requested behavior

Provide a way for a test to determine if the toMatchSnapshot comparison failed. This could be

  • an additional parameter to toMatchSnapshot. Or
  • having toMatchSnapshot set a variable that could be checked subsequently by the test. Or
  • some way for the caller to override the dontThrow method. Or
  • toMatchSnapshot returning a value indicating the comparison results. Or
  • an alternative to toMatchSnapshot that throws exceptions. Or.
  • a expect().doThrow(). prefix that lets toMatchSnapshot know that it should not call dontThrow
  • simply making the default toMatchSnapshot failure behavior to throw an exception like the other similar methods.

Benefits

  • Provides an easy way for callers to detect failure and alter the direction of a test.
  • Provides a way to halt the test immediately, allowing an engineer to inspect the state of the system immediately after the first sign of failure.
  • Provides consistency with the rest of the expect() methods
  • Provides better future-proofing than having the test author provide his own, 98% identical, implementation of toMatchSnapshot
@thymikee
Copy link
Collaborator

thymikee commented Jan 8, 2019

We already have .toThrowErrorMatchingSnapshot(), please see the docs: https://jestjs.io/docs/en/expect#tothrowerrormatchingsnapshot

@thymikee thymikee closed this as completed Jan 8, 2019
@jeysal
Copy link
Contributor

jeysal commented Jan 8, 2019

@thymikee this issue is actually about the expect(x).toMatchSnapshot() call itself throwing, not snapshotting the error thrown by a user-provided function.
@jasonnet if you're still interested in this, perhaps a concrete example would help understand why this could be useful in a test case.

@jasonnet
Copy link
Author

jasonnet commented Jan 8, 2019

@jeysal Thanks. You are correct that .toThrowErrorMatchingSnapshot doesn't appear to apply here because it seems to catch an exception, but the problem is that no exception is thrown.

It's been a while since I looked at this. This weekend I'll reboot my brain on jest and see if I can clarify further for @thymikee.

@thymikee
Copy link
Collaborator

thymikee commented Jan 8, 2019

Thanks!

@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