Slide 1

Slide 1 text

AN INTRODUCTION TO SNAPSHOT TESTING Freek Van der Herten

Slide 2

Slide 2 text

About me Freek Van der Herten Developer at SPATIE @freekmurze freek.dev spatie.be/products ohdear.app

Slide 3

Slide 3 text

spatie.be/open-source 300+ open source packages PHP & Laravel 175 million downloads total 9 million downloads per month

Slide 4

Slide 4 text

LET’S TALK ABOUT 
 SNAPSHOT TESTING

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Where does it come from? Characterisation testing Popularized by Facebook Jest Testing interfaces

Slide 7

Slide 7 text

How can we do it in PHP? github.com/spatie/phpunit-snapshot-assertions github.com/spatie/pest-plugin-snapshots assertMatchesSnapshot assertMatchesJsonSnapshot assertMatchesXmlSnapshot

Slide 8

Slide 8 text

DEMO

Slide 9

Slide 9 text

WRAPPING UP

Slide 10

Slide 10 text

Advantages Very easy to get started with Can be used to validate complex output Easily update all the tests in one go

Slide 11

Slide 11 text

Disadvantages Brittle Hard to pinpoint exact point of failure Bugs can be easily written in the snapshots

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

ONE MORE THING

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Thank you! spatie.be spatie.be/products spatie.be/open-source testing-laravel.com freek.dev