Slide 1

Slide 1 text

JEST can do whaaat? React Open Source Berlin 1 November 2017 @robinpokorny

Slide 2

Slide 2 text

JEST can do whaaat? React Open Source Berlin 1 November 2017 @robinpokorny Slides accompany a talk. Here, the talk is missing. For the full experience
 see the recording. I welcome any feedback! INFO

Slide 3

Slide 3 text

WHAT IS JEST delightful, zero configuration testing platform Jasmine’s and Expect’s successor practical utilities for awesome DX http:/ /facebook.github.io/jest/

Slide 4

Slide 4 text

SNAPSHOT TESTING

Slide 5

Slide 5 text

{JSON} A

Slide 6

Slide 6 text

{JSON} {JSON} A B

Slide 7

Slide 7 text

{JSON} {JSON} A B != !

Slide 8

Slide 8 text

TEST describe('method', () => { test('works', () => { const full = { number: 1975, bool: true, string: 'Hello, Vanek', promise: Promise.resolve('Better job'), symbol: Symbol('brewery'), [Symbol.for(‘brewmaster')]: 'Next beer!', … }; expect(full).toMatchSnapshot(); expect(1936).toMatchSnapshot(); }); });

Slide 9

Slide 9 text

SNAPSHOT // Jest Snapshot v1, https: //goo.gl/fbAQLP exports[`method works 1`] = ` Object { "bool": true, … "undefined": undefined, Symbol(brewmaster): "Next beer!", } `; exports[`method works 2`] = `1936`;

Slide 10

Slide 10 text

SNAPSHOT ` Object { "bool": true, "func": [Function], "map": Map { "position1" => "workman", "position2" => "stockkeeper", }, "null": null, "number": 1975, "promise": Promise {},

Slide 11

Slide 11 text

SNAPSHOT "set": Set { "think", "write", "snitch", }, "string": "Hello, Vanek", "symbol": Symbol(brewery), "undefined": undefined, Symbol(brewmaster): "Next beer!", } `

Slide 12

Slide 12 text

TEST describe('method', () => { test('works', () => { const full = { … }; expect(full).toMatchSnapshot('new name'); }); }); SNAPSHOT exports[`new name 1`] = `…`

Slide 13

Slide 13 text

TEST test('mock function', () => { const fn = jest.fn(); fn('Vanek'); fn('Ferdinand', 'Vanek'); expect(fn).toHaveBeenCalledWith('Vanek'); expect(fn).toHaveBeenCalledWith('Ferdinand', 'Vanek'); expect(fn).toHaveBeenCalledTimes(2); // vs expect(fn.mock.calls).toMatchSnapshot(); });

Slide 14

Slide 14 text

SNAPSHOT exports[`method mock function 1`] = ` Array [ Array [ "Vanek", ], Array [ "Ferdinand", "Vanek", ], ] `;

Slide 15

Slide 15 text

?

Slide 16

Slide 16 text

SNAPSHOT exports[`component with defaults`] = `

Slide 17

Slide 17 text

TEST SNAPSHOT exports[`method immutable 1`] = ` Immutable.Map { "a": 1, "b": 2, } `; expect(Immutable.Map({ a: 1, b: 2 })).toMatchSnapshot();

Slide 18

Slide 18 text

const plugin = { test(val) { return val && val.isPlay; }, serialize(val, config, indent, depth, refs, printer) { const name = val.constructor.name; const newIndent = indent + config.indent; return ( `Play <${val.title}>: ` + printer(val.content, config, newIndent, depth ++, refs) ); } }; SERIALISER

Slide 19

Slide 19 text

SERIALISER expect.addSnapshotSerializer(plugin); // package.json { ... "jest": { "snapshotSerializers": ["plugin.js"] } } or

Slide 20

Slide 20 text

TEST test('play', () => { const play = { isPlay: true, title: 'Audience', content: { scenes: 1 } }; expect([ play ]).toMatchSnapshot(); });

Slide 21

Slide 21 text

SNAPSHOT exports[`method play 1`] = ` Array [ Play : Object { "scenes": 1, }, ] `;

Slide 22

Slide 22 text

TEST test('diff', () => { const play = { title: 'Audience', characters: 2 }; expect(play).toMatchSnapshot(); expect({ ...play, characters: 3 }).toMatchSnapshot(); });

Slide 23

Slide 23 text

SNAPSHOT exports[`diff 1`] = ` Object { "characters": 2, "title": "Audience", } `; exports[`diff 2`] = ` Object { "characters": 3, "title": "Audience", } `;

Slide 24

Slide 24 text

const { toMatchDiffSnapshot } = require('snapshot-diff'); expect.extend({ toMatchDiffSnapshot }); test('diff', () => { const play = { title: 'Audience', characters: 2 }; expect(play) .toMatchDiffSnapshot({ ...play, characters: 3 }); });

Slide 25

Slide 25 text

SNAPSHOT exports[`diff 1`] = ` "Snapshot Diff: - First value + Second value Object { - \\"characters \\": 2, + \\"characters \\": 3, \\"title \\": \\"Audience \\", }" `;

Slide 26

Slide 26 text

structures concurrent or after whole algorithms write before part TDD SNAPSHOTS ✖ inside codebase

Slide 27

Slide 27 text

watch

Slide 28

Slide 28 text

$ jest --watch $ npm test -- --watch

Slide 29

Slide 29 text

understands dependencies filter by Path or Test name Update snapshots failed re-run first

Slide 30

Slide 30 text

// moduleA.spec.js const moduleA = require('./moduleA'); test('moduleA', () => { expect(moduleA()).toBe(true); }); // moduleA.js const moduleB = require('./moduleB'); DEPS

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

in JEST ASYNC testing

Slide 34

Slide 34 text

test('promise', done => { Promise.resolve(7) .then(n => { expect(n).toBe(7); }) .then(done) .catch(done.fail); }); CALLBACK

Slide 35

Slide 35 text

PROMISES test('promises', () => { Promise.resolve(8).then(n => { expect(n).toBe(7); }); return Promise.resolve(7).then(n => { expect(n).toBe(7); }); });

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

REJECTION test('reject', () => { return Promise.reject(0).catch(n => { expect(n).toBe(0); }); });

Slide 39

Slide 39 text

REJECTION test('reject', () => { return Promise.reject(0).catch(n => { expect(n).toBe(0); }); }); test('reject', () => { return Promise.resolve(7).catch(n => { expect(n).toBe(0); }); });

Slide 40

Slide 40 text

REJECTION test('reject', () => { expect.assertions(1); return Promise.resolve(7) .then(() => { throw new Error('Not rejected!'); }) .catch(n => { expect(n).toBe(0); }); });

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

ASYNC
 AWAIT test('async', async () => { const n = await Promise.resolve(7) const m = await Promise.resolve(42) expect(n).toBe(7) expect(m).toBe(42) })

Slide 43

Slide 43 text

ASYNC
 AWAIT test('async', async () => { const n = Promise.resolve(7) const m = await Promise.resolve(42) expect(n).toBe(7) expect(m).toBe(42) })

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

ASYNC REJECTION test('async rejection', async () => { try { await Promise.reject(0); } catch (e) { expect(e).toBe(0); } });

Slide 46

Slide 46 text

ASYNC REJECTION test('async rejection', async () => { try { await Promise.resolve(7); } catch (e) { expect(e).toBe(0); } });

Slide 47

Slide 47 text

ASYNC REJECTION test('async rejection', async () => { try { await Promise.resolve(0); expect(true).toBe(false); } catch (e) { expect(e).toBe(0); } });

Slide 48

Slide 48 text

.RESOLVES
 & .REJECTS test('resolves/rejects', async () => { await expect(Promise.resolve(7)).resolves.toBe(7); await expect(Promise.reject(0)).rejects.not.toBe(7); });

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

JEST can do whaaat? will do

Slide 51

Slide 51 text

MOCK NAME test('mockName', () => { const mockFn = jest.fn(); expect(mockFn).toHaveBeenCalled(); });

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

MOCK NAME test('mockName', () => { const mockFn = jest.fn(); expect(mockFn).toHaveBeenCalled(); }); test('mockName', () => { const mockFn = jest.fn().mockName('mockedFunction'); expect(mockFn).toHaveBeenCalled(); });

Slide 54

Slide 54 text

GIF not supported in PDF INFO

Slide 55

Slide 55 text

$ jest packages/moduleA $ jest packages/moduleA --passWithNoTests

Slide 56

Slide 56 text

by @kentcdodds GIF not supported in PDF INFO

Slide 57

Slide 57 text

RELATED • github.com/robinpokorny/jest-can-do-whaaat • Jest Snapshots and Beyond by Rogelio Guzman (recording) • Writing snapshot plugins in the docs • Effective Snapshot Testing by Kent C. Dodds • Async testing in Jest (recording) • Snapshot testing in Jest (recording) • Async testing Koa with Jest Full links in the description INFO

Slide 58

Slide 58 text

JEST can do whaaat? @robinpokorny Help others to go