Slide 1

Slide 1 text

Oh Snap! Gaining leverage over legacy CLI apps with snapshot testing Dan Miller Rogue Software Engineer @jazzdan https://dmiller.io

Slide 2

Slide 2 text

@jazzdan Overview •What are snapshot tests? •How do you use them? •A tale from production!

Slide 3

Slide 3 text

Who is this person?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

@jazzdan I love testing

Slide 9

Slide 9 text

Snapshot Testing

Slide 10

Slide 10 text

Code Produces Output

Slide 11

Slide 11 text

@jazzdan Assert that the output doesn’t change

Slide 12

Slide 12 text

Code Produces Output

Slide 13

Slide 13 text

Code Produces Output

Slide 14

Slide 14 text

@jazzdan Why is this especially useful for React?

Slide 15

Slide 15 text

@jazzdan If the output changed, then your UI changed

Slide 16

Slide 16 text

< A /> < C /> < D />

Slide 17

Slide 17 text

< A /> < C /> < D />

Slide 18

Slide 18 text

< A /> < B /> < C /> < D /> < D />

Slide 19

Slide 19 text

< A /> < B /> < C /> < D /> < D />

Slide 20

Slide 20 text

< A /> < B /> < C /> < D /> < D />

Slide 21

Slide 21 text

Code Produces Output

Slide 22

Slide 22 text

JSX Renders to DOM

Slide 23

Slide 23 text

JSX Renders to DOM

Slide 24

Slide 24 text

JSX Renders to DOM Text!

Slide 25

Slide 25 text

Code Produces Serializable Output

Slide 26

Slide 26 text

@jazzdan Show me the code!

Slide 27

Slide 27 text

1 test("Link renders correctly", () => { 2 const tree = renderer 3 .create( 4 Queens JS) 5 .toJSON(); 6 expect(tree).toMatchSnapshot(); 7 });

Slide 28

Slide 28 text

PASS ./Link.test.js ✓ renders correctly (15ms) Snapshot Summary › 1 snapshot written in 1 test suite. Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 1 added, 1 total Time: 0.287s Ran all test suites related to changed files.

Slide 29

Slide 29 text

1 exports[`Link renders correctly 1`] = ` 2 6 Queens JS 7 8 `;

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Non-React Uses

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

@jazzdan We wrote a web application

Slide 35

Slide 35 text

@jazzdan We wrote a web application text editor

Slide 36

Slide 36 text

@jazzdan We wrote a web application text editor database

Slide 37

Slide 37 text

@jazzdan We wrote a web application text editor database
 compiler

Slide 38

Slide 38 text

App Changed daily JSON Generated quarterly

Slide 39

Slide 39 text

Node CLI App JSON JSON PHP SQL

Slide 40

Slide 40 text

Code Produces Output

Slide 41

Slide 41 text

Node CLI App JSON JSON PHP SQL

Slide 42

Slide 42 text

Node CLI App JSON PHP SQL

Slide 43

Slide 43 text

Node CLI App JSON JSON PHP SQL

Slide 44

Slide 44 text

Node CLI App JSON PHP SQL Intermediate Representation

Slide 45

Slide 45 text

Node CLI App Intermediate Representation

Slide 46

Slide 46 text

@jazzdan It’s OK if the test fails!

Slide 47

Slide 47 text

@jazzdan Tests tell you about change in the behavior of your system not necessarily a bug

Slide 48

Slide 48 text

@jazzdan Snapshot tests are great for detecting changes

Slide 49

Slide 49 text

@jazzdan Example Uses •CLI help menus •Error messages •Emails •CSVs, SVGs, etc

Slide 50

Slide 50 text

Integrating in to your test runner

Slide 51

Slide 51 text

@jazzdan npm install jest-snapshot

Slide 52

Slide 52 text

// ... const toMatchSnapshot = function(received: any, testName?: string) { this.dontThrow && this.dontThrow(); const {currentTestName, isNot, snapshotState}: MatcherState = this; if (isNot) { throw new Error('Jest: `.not` cannot be used with `.toMatchSnapshot()`.'); } if (!snapshotState) { throw new Error('Jest: snapshot state must be initialized.'); } const result = snapshotState.match( testName || currentTestName || '', received, );

Slide 53

Slide 53 text

class SnapshotState { _counters: Map; _dirty: boolean; _index: number; _updateSnapshot: SnapshotUpdateState; _snapshotData: {[key: string]: string}; _snapshotPath: Path; _uncheckedKeys: Set; added: number; expand: boolean; matched: number; unmatched: number; updated: number; }

Slide 54

Slide 54 text

Snapshots built-in

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

@jazzdan Thanks! Find me over a if you love testing too!

Slide 58

Slide 58 text

@jazzdan More Resources • https://www.selenic.com/blog/?p=663 • https://glebbahmutov.com/blog/snapshot-testing/