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 Request: Snapshot Schema Validation #38

Open
BadAsstronaut opened this issue Mar 25, 2018 · 2 comments
Open

Feature Request: Snapshot Schema Validation #38

BadAsstronaut opened this issue Mar 25, 2018 · 2 comments
Labels

Comments

@BadAsstronaut
Copy link

Hi, thank you for creating this library!

I am using snapshottest on a project that has a level of randomization to test interdependent relationships. It would be helpful if snapshottest could validate the schema of the test result instead of the full text.

This could be achieved by adding a snapshot.assert_schema_match(format='json', test_result) or something similar.

@BadAsstronaut BadAsstronaut changed the title Feature Request: Feature Request: Snapshot Schema Validation Mar 25, 2018
@paulmelnikow
Copy link
Collaborator

paulmelnikow commented Sep 29, 2020

This is similar to the request in #96.

I wonder if we could solve both issues by adding a parameter to assert_match that lets you pass your own comparator.

@medmunds
Copy link
Collaborator

medmunds commented Oct 1, 2020

I like the idea of custom comparators. It could also address the various "ignore some fields" needs (#21, #32, #102, et al).

JavaScript's jest-snapshot (the inspiration for this package) handles this problem through "asymmetric property matchers". You effectively declare a schema inline in your snapshot test assertion, by placing their any matchers within objects, arrays, and constants that define the (partial) expected shape. (Anything not specified by an any must match the snapshot exactly.)

callee is a Python package that implements something similar, intended for use with mock. I haven't tried it myself yet (just discovered it today), but it looks useful. I wonder how much work it would take to get callee's matchers working with snapshottest? Something like...

    snapshot.assert_match(my_api_response, match={
        "id": callee.Integer(),
        "history": {  # nesting allowed...
            "created_at": callee.InstanceOf(datetime),
            "edit_timestamps": callee.List(callee.Integer()),
        },
        # everything else in my_api_response
        # must exactly match the snapshot
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants