• jjjalljs@ttrpg.network
    link
    fedilink
    English
    arrow-up
    1
    ·
    5 days ago

    Snapshot tests are an anti-pattern. Easy to use, lead to problems.

    They do not encode intent. Everything in the snapshot is equally weighted. Your test will fail the same way if your copy changes or your HTML structure, but one of those is probably way more important. Assert on what’s important.

    They discourage thinking about what you’re doing. Suppose you have an API that returns information about a user and their pets. Your first integration test adds a user to the database, makes a request, and looks at the response. You could just brainlessly snapshot the response, but the user PK in the response might not always be the same. People are going to try to snapshot the response anyway.

    Humans are lazy, and they won’t read the diff carefully. This is especially true if the diff is large or complex. Doubly so if you have a great number of these. They’ll go “oh yeah I changed this component so it should have changed. Accept changes” and move on. No one wants to look at 40 large dozen line diffs.

    If you have your code set up to arrange and then act, you’re like 80% of the way there to writing a normal set of asserts.

    I would never recommend snapshot testing.