Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An Introduction to Snapshot testing

An Introduction to Snapshot testing

Freek Van der Herten

September 01, 2021
Tweet

More Decks by Freek Van der Herten

Other Decks in Technology

Transcript

  1. AN INTRODUCTION


    TO SNAPSHOT TESTING
    Freek Van der Herten

    View Slide

  2. About me


    Freek Van der Herten


    Developer at SPATIE


    @freekmurze


    freek.dev


    spatie.be/products


    ohdear.app

    View Slide

  3. spatie.be/open-source


    300+ open source packages


    PHP & Laravel


    175 million downloads total


    9 million downloads per month

    View Slide

  4. LET’S TALK ABOUT

    SNAPSHOT TESTING

    View Slide

  5. What?


    A file on disk – let’s call it a snapshot


    Test succeeds if the output matches the snapshot


    If not, the test will fail

    View Slide

  6. Where does it come from?


    Characterisation testing


    Popularized by Facebook


    Jest


    Testing interfaces

    View Slide

  7. How can we do it in PHP?


    github.com/spatie/phpunit-snapshot-assertions


    github.com/spatie/pest-plugin-snapshots


    assertMatchesSnapshot

    assertMatchesJsonSnapshot

    assertMatchesXmlSnapshot

    View Slide

  8. DEMO

    View Slide

  9. WRAPPING UP

    View Slide

  10. Advantages


    Very easy to get started with


    Can be used to validate complex output


    Easily update all the tests in one go


    View Slide

  11. Disadvantages


    Brittle


    Hard to pinpoint exact point of failure


    Bugs can be easily written in the snapshots

    View Slide

  12. Interesting links


    github.com/spatie/phpunit-snapshot-assertions


    github.com/spatie/pest-plugin-snapshots


    github.com/spatie/shiki-php


    github.com/spatie/laravel-markdown


    torchlight.dev

    View Slide

  13. ONE MORE THING

    View Slide

  14. testing-laravel.com


    Premium video course, 6+ hours of video


    Learn how to tests a Laravel app from scratch


    Tips and tricks to sharpen your skills


    Two courses in one: Pest & PHPUnit


    10 % discount with SPATIE-LOVES-LARACON


    View Slide

  15. Thank you!


    spatie.be


    spatie.be/products


    spatie.be/open-source


    testing-laravel.com


    freek.dev

    View Slide