Slide 1

Slide 1 text

ASSERTIONS codurance.com Improving testing

Slide 2

Slide 2 text

Matheus Marabesi Hello there, you can call me Marabesi, But my name is Matheus Marabesi, I work at Codurance as a Software craftsperson. I enjoy talking about anything related to: testing, patterns and gamification. You can find me at @MatheusMarabesi or https://marabesi.com Codurance Crafting Code

Slide 3

Slide 3 text

1. Intro - Background 2. Assertion smells 3. Assertion API 4. Exploring assertions 5. Wrapping up Crafting code Agenda

Slide 4

Slide 4 text

1. Intro - Background 2. Assertion smells 3. Assertion API - Jest 4. Exploring assertions 5. Wrapping up Crafting code Agenda

Slide 5

Slide 5 text

1. Background Warming up Getting started

Slide 6

Slide 6 text

JOURNEY This talk is based on my own

Slide 7

Slide 7 text

TDD

Slide 8

Slide 8 text

https://app.livestorm.co/codurance/testing-anti-patterns-workshop

Slide 9

Slide 9 text

Jest - Framework

Slide 10

Slide 10 text

Jest - Framework

Slide 11

Slide 11 text

ANY - Framework

Slide 12

Slide 12 text

ANY - Framework

Slide 13

Slide 13 text

toEqual usually is the first assertion used❗

Slide 14

Slide 14 text

🤔 And, it's something like . . .

Slide 15

Slide 15 text

Assert test('assert some value', () => { const myVar = 'some val' expect(myVar).toEqual('some val') })

Slide 16

Slide 16 text

Assert test('assert some value', () => { const myVar = 'some val' expect(myVar).toEqual('some val') ✅ })

Slide 17

Slide 17 text

Assert test('light is on', () => { const off = false expect(off === false).toEqual(true) })

Slide 18

Slide 18 text

Assert test('light is on', () => { const off = false expect(off === false).toEqual(true) ✅ })

Slide 19

Slide 19 text

Assert test('it has two users', () => { const users = [ { name: 'jhon'}, { name: 'maria' } ] expect(users.length).toEqual(2) })

Slide 20

Slide 20 text

Assert test('it has two users', () => { const users = [ { name: 'jhon'}, { name: 'maria' } ] expect(users.length).toEqual(2) ✅ })

Slide 21

Slide 21 text

2. Assertion smells Smells are something that developers relate to, has some insight that it's not the best approach and still can't figure out what. Crafting code

Slide 22

Slide 22 text

Assertion smells const myVar = 'some val' expect(myVar).equals('some val') ✅ const off = false expect(off === false).equals(true) ✅ const users = [ { name: 'jhon'}, { name: 'maria' } ] expect(users.length).equals(2) ✅

Slide 23

Slide 23 text

Assertion smells const myVar = 'some val' expect(myVar).equals('some val') ✅ const off = false expect(off === false).equals(true) ✅ const users = [ { name: 'jhon'}, { name: 'maria' } ] expect(users.length).equals(2) ✅

Slide 24

Slide 24 text

Assertion smells const myVar = 'some val' expect(myVar).equals('some val') ✅ const off = false expect(off === false).equals(true) ✅ const users = [ { name: 'jhon'}, { name: 'maria' } ] expect(users.length).equals(2) ✅

Slide 25

Slide 25 text

Assertion smells const myVar = 'some val' expect(myVar).equals('some val') ✅ const off = false expect(off === false).equals(true) ✅ const users = [ { name: 'jhon'}, { name: 'maria' } ] expect(users.length).equals(2) ✅

Slide 26

Slide 26 text

Assertion smells const myVar = 'some val' expect(myVar).equals('some val') ✅ const off = false expect(off === false).equals(true) ✅ const users = [ { name: 'jhon'}, { name: 'maria' } ] expect(users.length).equals(2) ✅

Slide 27

Slide 27 text

When Test Driven Development Goes Wrong Dave Farley - by Continuous Delivery

Slide 28

Slide 28 text

A more expressive way? 🤔 IS THERE

Slide 29

Slide 29 text

3. Assertion API 5⃣ Meeting the jest assertion API Crafting code

Slide 30

Slide 30 text

Primitive assertions 🧱 Crafting code

Slide 31

Slide 31 text

Modifiers 🪣 Crafting code

Slide 32

Slide 32 text

Callbacks 📱 Crafting code

Slide 33

Slide 33 text

Timers ⏰ Crafting code

Slide 34

Slide 34 text

Async 🏹 Crafting code

Slide 35

Slide 35 text

Primitive assertions 🧱 Modifiers 🪣 Callbacks 📱 Timers ⏰ Async 🏹 Crafting code

Slide 36

Slide 36 text

4. Exploring assertions Primitive, modifier, callback, timer and async. Crafting code

Slide 37

Slide 37 text

Primitive assertions 🧱 Numbers, Booleans and Arrays Crafting code

Slide 38

Slide 38 text

test('it returns a number', () => { const isNumber = number => number expect(typeof isNumber(2)).toEqual('number') }) test('it returns a number', () => { const isNumber = number => number expect(isNumber(2)).toEqual(expect.any(Number)) }) Numbers 🧱

Slide 39

Slide 39 text

test('it returns a number', () => { const isNumber = number => number expect(typeof isNumber(2)).toEqual('number') }) test('it returns a number', () => { const isNumber = number => number expect(isNumber(2)).toEqual(expect.any(Number)) }) Numbers 🧱

Slide 40

Slide 40 text

test('it returns a number', () => { const isNumber = number => number expect(typeof isNumber(2)).toEqual('number') }) test('it returns a number', () => { const isNumber = number => number expect(isNumber(2)).toEqual(expect.any(Number)) }) Numbers 🧱

Slide 41

Slide 41 text

test('it returns a number', () => { const isNumber = number => number expect(typeof isNumber(2)).toEqual('number') }) test('it returns a number', () => { const isNumber = number => number expect(isNumber(2)).toEqual(expect.any(Number)) }) Numbers 🧱

Slide 42

Slide 42 text

test('expected is greater than the desired', () => { const expected = 10 const actual = 3 expect(expected > actual).toEqual(true) }) test('expected is greater than the desired', () => { const expected = 10 const actual = 3 expect(expected).toBeGreaterThan(actual) }) Booleans 🧱

Slide 43

Slide 43 text

test('expected is greater than the desired', () => { const expected = 10 const actual = 3 expect(expected > actual).toEqual(true) }) test('expected is greater than the desired', () => { const expected = 10 const actual = 3 expect(expected).toBeGreaterThan(actual) }) Booleans 🧱

Slide 44

Slide 44 text

test('expected is greater than the desired', () => { const expected = 10 const actual = 3 expect(expected > actual).toEqual(true) }) test('expected is greater than the desired', () => { const expected = 10 const actual = 3 expect(expected).toBeGreaterThan(actual) }) Booleans 🧱

Slide 45

Slide 45 text

test('expected is greater than the desired', () => { const expected = 10 const actual = 3 expect(expected > actual).toEqual(true) }) test('expected is greater than the desired', () => { const expected = 10 const actual = 3 expect(expected).toBeGreaterThan(actual) }) Booleans 🧱

Slide 46

Slide 46 text

test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits[0]).toEqual('banana') expect(expectedFruits[1]).toEqual('mango') expect(expectedFruits[2]).toEqual('watermelon') }) test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] const actualFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits).toEqual( expect.arrayContaining(actualFruits) ) }) Arrays 🧱

Slide 47

Slide 47 text

test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits[0]).toEqual('banana') expect(expectedFruits[1]).toEqual('mango') expect(expectedFruits[2]).toEqual('watermelon') }) test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] const actualFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits).toEqual( expect.arrayContaining(actualFruits) ) }) Arrays 🧱

Slide 48

Slide 48 text

test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits[0]).toEqual('banana') expect(expectedFruits[1]).toEqual('mango') expect(expectedFruits[2]).toEqual('watermelon') }) test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] const actualFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits).toEqual( expect.arrayContaining(actualFruits) ) }) Arrays 🧱

Slide 49

Slide 49 text

test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits[0]).toEqual('banana') expect(expectedFruits[1]).toEqual('mango') expect(expectedFruits[2]).toEqual('watermelon') }) test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] const actualFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits).toEqual( expect.arrayContaining(actualFruits) ) }) Arrays 🧱

Slide 50

Slide 50 text

test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits[0]).toEqual('banana') expect(expectedFruits[1]).toEqual('mango') expect(expectedFruits[2]).toEqual('watermelon') }) test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] const actualFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits).toEqual(expect.arrayContaining(actualFruits)) }) Arrays 🧱

Slide 51

Slide 51 text

test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits[0]).toEqual('banana') expect(expectedFruits[1]).toEqual('mango') expect(expectedFruits[2]).toEqual('watermelon') }) test('list three fruits', () => { const expectedFruits = ['banana', 'mango', 'watermelon'] const actualFruits = ['banana', 'mango', 'watermelon'] expect(expectedFruits).toEqual(expect.arrayContaining(actualFruits)) }) Arrays 🧱

Slide 52

Slide 52 text

test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList.length).toEqual(3) }) test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList).toHaveLength(3) }) Arrays 🧱

Slide 53

Slide 53 text

test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList.length).toEqual(3) }) test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList).toHaveLength(3) }) Arrays 🧱

Slide 54

Slide 54 text

test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList.length).toEqual(3) }) test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList).toHaveLength(3) }) Arrays 🧱

Slide 55

Slide 55 text

test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList.length).toEqual(3) }) test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList).toHaveLength(3) }) Arrays 🧱

Slide 56

Slide 56 text

test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList.length).toEqual(3) }) test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList).toHaveLength(3) }) Arrays 🧱

Slide 57

Slide 57 text

test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList.length).toEqual(3) }) test('list with three numbers', () => { const myList = [1, 2, 3] expect(myList).toHaveLength(3) }) Arrays 🧱

Slide 58

Slide 58 text

Modifiers 🪣 Not Crafting code

Slide 59

Slide 59 text

test('light is on', () => { const isOff = false expect(!isOff).toBe(true) }) test('light is on', () => { const isOff = false expect(isOff).not.toBe(true) }) Not 🪣

Slide 60

Slide 60 text

test('light is on', () => { const isOff = false expect(!isOff).toBe(true) }) test('light is on', () => { const isOff = false expect(isOff).not.toBe(true) }) Not 🪣

Slide 61

Slide 61 text

test('light is on', () => { const isOff = false expect(!isOff).toBe(true) }) test('light is on', () => { const isOff = false expect(isOff).not.toBe(true) }) Not 🪣

Slide 62

Slide 62 text

test('light is on', () => { const isOff = false expect(!isOff).toBe(true) }) test('light is on', () => { const isOff = false expect(isOff).not.toBe(true) }) Not 🪣

Slide 63

Slide 63 text

test('light is on', () => { const isOff = false expect(!isOff).toBe(true) }) test('light is on', () => { const isOff = false expect(isOff).not.toBe(true) }) Not 🪣

Slide 64

Slide 64 text

test('light is on', () => { const isOff = false expect(!isOff).toBe(true) }) test('light is on', () => { const isOff = false expect(isOff).not.toBe(true) }) Not 🪣

Slide 65

Slide 65 text

Not in the document example Not

Slide 66

Slide 66 text

Callbacks 📱 Functions Crafting code

Slide 67

Slide 67 text

test('callback has been invoked', done => { callAsyncFunc(() => { expect(true).toEqual(true) done() }) }) test('callback has been invoked', () => { const result = jest.fn() callAsyncFunc(result) expect(result).toHaveBeenCalled() }) Callbacks 📱 const callAsyncFunc = cb => cb();

Slide 68

Slide 68 text

test('callback has been invoked', done => { callAsyncFunc(() => { expect(true).toEqual(true) done() }) }) test('callback has been invoked', () => { const result = jest.fn() callAsyncFunc(result) expect(result).toHaveBeenCalled() }) Callbacks 📱 const callAsyncFunc = cb => cb();

Slide 69

Slide 69 text

test('callback has been invoked', done => { callAsyncFunc(() => { expect(true).toEqual(true) done() }) }) test('callback has been invoked', () => { const result = jest.fn() callAsyncFunc(result) expect(result).toHaveBeenCalled() }) Callbacks 📱 const callAsyncFunc = cb => cb();

Slide 70

Slide 70 text

test('callback has been invoked', done => { callAsyncFunc(() => { expect(true).toEqual(true) done() }) }) test('callback has been invoked', () => { const result = jest.fn() callAsyncFunc(result) expect(result).toHaveBeenCalled() }) Callbacks 📱 const callAsyncFunc = cb => cb();

Slide 71

Slide 71 text

test('callback has been invoked', done => { callAsyncFunc(() => { expect(true).toEqual(true) done() }) }) test('callback has been invoked', () => { const result = jest.fn() callAsyncFunc(result) expect(result).toHaveBeenCalled() }) Callbacks 📱 const callAsyncFunc = cb => cb();

Slide 72

Slide 72 text

test('callback has been invoked', done => { callAsyncFunc(() => { expect(true).toEqual(true) done() }) }) test('callback has been invoked', () => { const result = jest.fn() callAsyncFunc(result) expect(result).toHaveBeenCalled() }) Callbacks 📱 const callAsyncFunc = cb => cb();

Slide 73

Slide 73 text

test('callback has been invoked', done => { callAsyncFunc(() => { expect(true).toEqual(true) done() }) }) test('callback has been invoked', () => { const result = jest.fn() callAsyncFunc(result) expect(result).toHaveBeenCalled() }) Callbacks 📱 const callAsyncFunc = cb => cb();

Slide 74

Slide 74 text

Timers ⏰ setTimeout, setInterval, clearTimeout, clearInterval Crafting code

Slide 75

Slide 75 text

Asynchronous Behavior Eradicating Non-Determinism in Tests Martin Fowler, 2011

Slide 76

Slide 76 text

function callAsyncFunction() { return new Promise((resolve) => { setTimeout(() => { resolve(true); }, 1000); }); } test('assert after some time', done => { callAsyncFunction(). then((value) => { expect(value).toBe(true) done() }) }) Timers ⏰

Slide 77

Slide 77 text

function callAsyncFunction() { return new Promise((resolve) => { setTimeout(() => { resolve(true); }, 1000); }); } test('assert after some time', done => { callAsyncFunc(). then((value) => { expect(value).toBe(true) done() }) }) Timers ⏰

Slide 78

Slide 78 text

function callAsyncFunction() { return new Promise((resolve) => { setTimeout(() => { resolve(true); }, 1000); }); } test('assert after some time', done => { callAsyncFunction(). then((value) => { expect(value).toBe(true) done() }) }) Timers ⏰

Slide 79

Slide 79 text

describe('using fake timers', () => { beforeEach(() => { jest.useFakeTimers() }) afterEach(() => { jest.useRealTimers() }) }) Timers ⏰

Slide 80

Slide 80 text

test("should handle next scene", async () => { const value = callAsyncFunction(); jest.advanceTimersByTime(2000); expect(await value).toBe(true); }); Timers ⏰

Slide 81

Slide 81 text

test("should handle next scene", async () => { const value = callAsyncFunction(); jest.advanceTimersByTime(2000); expect(await value).toBe(true); }); Timers ⏰

Slide 82

Slide 82 text

test("should handle next scene", async () => { const value = callAsyncFunction(); jest.advanceTimersByTime(2000); expect(await value).toBe(true); }); Timers ⏰

Slide 83

Slide 83 text

test("should handle next scene", async () => { const value = callAsyncFunction(); jest.advanceTimersByTime(2000); expect(await value).toBe(true); }); Timers ⏰

Slide 84

Slide 84 text

test("should handle next scene", async () => { const value = callAsyncFunction(); jest.advanceTimersByTime(2000); expect(await value).toBe(true); }); Timers ⏰

Slide 85

Slide 85 text

test("should handle next scene", async () => { const value = callAsyncFunction(); jest.advanceTimersByTime(2000); expect(await value).toBe(true); }); Timers ⏰

Slide 86

Slide 86 text

Async 🏹 Promises Crafting code

Slide 87

Slide 87 text

test('my async test with then', done => { callAsyncFunction(). then((value) => { expect(value).toBe(true) done() }) }) test('my async test', async () => { await expect(callAsyncFunction()).resolves.toEqual(true) }) test('my async test rejects', async () => { await expect(rejectsAsyncFunction()).rejects.toEqual(false) }) Promises 🏹

Slide 88

Slide 88 text

test('my async test with then', done => { callAsyncFunction(). then((value) => { expect(value).toBe(true) done() }) }) test('my async test', async () => { await expect(callAsyncFunction()).resolves.toEqual(true) }) test('my async test rejects', async () => { await expect(rejectsAsyncFunction()).rejects.toEqual(false) }) Promises 🏹

Slide 89

Slide 89 text

test('my async test with then', done => { callAsyncFunction(). then((value) => { expect(value).toBe(true) done() }) }) test('my async test', async () => { await expect(callAsyncFunction()).resolves.toEqual(true) }) test('my async test rejects', async () => { await expect(rejectsAsyncFunction()).rejects.toEqual(false) }) Promises 🏹

Slide 90

Slide 90 text

test('my async test with then', done => { callAsyncFunction(). then((value) => { expect(value).toBe(true) done() }) }) test('my async test', async () => { await expect(callAsyncFunction()).resolves.toEqual(true) }) test('my async test rejects', async () => { await expect(rejectsAsyncFunction()).rejects.toEqual(false) }) Promises 🏹

Slide 91

Slide 91 text

test('my async test with then', done => { callAsyncFunction(). then((value) => { expect(value).toBe(true) done() }) }) test('my async test', async () => { await expect(callAsyncFunction()).resolves.toEqual(true) }) test('my async test rejects', async () => { await expect(rejectsAsyncFunction()).rejects.toEqual(false) }) Promises 🏹

Slide 92

Slide 92 text

6. Wrapping up We are almost done! Crafting code

Slide 93

Slide 93 text

● toEqual is usually used more than any assertion ● Assertion smells ● Using different assertions can improve understanding ● 🧱, 🪣,📱, ⏰, 🏹 What we covered

Slide 94

Slide 94 text

What we covered HAPPY TESTING 🤪

Slide 95

Slide 95 text

Matheus Marabesi Hello there, you can call me Marabesi, But my name is Matheus Marabesi, I work at Codurance as a Software craftsperson. I enjoy talking about anything related to: testing, patterns and gamification. You can find me at @MatheusMarabesi or https://marabesi.com Codurance Crafting Code