Slide 1

Slide 1 text

NodeConf EU 2023 100% test coverage The road to ruyadorno.com

Slide 2

Slide 2 text

The road to 100% test coverage Ruy Adorno Senior Software Developer working at Google Node.js TSC NodeConf EU 2023 ruyadorno.com

Slide 3

Slide 3 text

TOC Setup the work environment for the workshop Understanding test coverage Techniques Questions / Consultation The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 4

Slide 4 text

Setup Install node 20 (or 18): https://nodejs.org/en/download Open your terminal, navigate to a suitable folder, e.g: cd ~/tmp/100-workshop Run the workshop generator: npm init @ruyadorno/100-workshop@latest Run npm install and tests: npm it The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 5

Slide 5 text

What’s in it? The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 6

Slide 6 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 7

Slide 7 text

Give it a try: List all LTS releases: node cli.js --filter=lts Last month’s releases: node cli.js --recent=month Only return the resulting count: node cli.js --count The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 8

Slide 8 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 9

Slide 9 text

What is a test A way to programatically confirm your program does what you think it does. If your tests are also a program, what validates that program? Surprise: It’s you, the developer! The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 10

Slide 10 text

Test Coverage Tracking coverage is sort of a meta test It tracks what parts of the program are executed Gives you a hint of how much of a test it really is Definitely does not prove that either the source code OR the test code are good The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 11

Slide 11 text

100% Test Coverage It means you covered all lines, functions and statements Still not enough, 100% code coverage does NOT mean 100% of features tested Gives you super powers The road to 100% test coverage NodeConf EU 2023 ruyadorno.com 🦸

Slide 12

Slide 12 text

Notice: This workshop has a cheat code available To quickly complete a step, it’s possible to use the same generator ran to create the boilerplate. Run the generator using the step option. In this example we’re using the generator to complete Step 1: npx @ruyadorno/create-100-workshop --step=1 The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ⚠

Slide 13

Slide 13 text

Step 1 Let’s add an initial test to the main index.js module! The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 14

Slide 14 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ./test/index.js ۪ ۪ $ npx @ruyadorno/create-100-workshop --step=1 ./src/index.js

Slide 15

Slide 15 text

Step 2 Let’s avoid the network request The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 16

Slide 16 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ./test/index.js ۪ ۪ $ npx @ruyadorno/create-100-workshop —step=2

Slide 17

Slide 17 text

Let’s visualize better the coverage results: Opens an html-based interface in your default browser: npm t -- --coverage-report=html The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 18

Slide 18 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 19

Slide 19 text

Step 3 Let’s add tests for format-count.js The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 20

Slide 20 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ./test/format-count.js ۪ ۪ $ npx @ruyadorno/create-100-workshop —step=3

Slide 21

Slide 21 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com 🤩

Slide 22

Slide 22 text

Step 4 Let’s refactor and add tests for filter-recent.js The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 23

Slide 23 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ./src/format-recent.js ۪ ۪ $ npx @ruyadorno/create-100-workshop —step=4 ./src/date.js

Slide 24

Slide 24 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ./test/format-recent.js ۪ ۪ $ npx @ruyadorno/create-100-workshop —step=4

Slide 25

Slide 25 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 26

Slide 26 text

Step 5 Let’s add tests for filter-npm.js The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 27

Slide 27 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ./test/filter-npm.js ۪ ۪ $ npx @ruyadorno/create-100-workshop —step=5

Slide 28

Slide 28 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 29

Slide 29 text

Step 6 Let’s add tests for filter-booleans.js The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 30

Slide 30 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ./test/filter-booleans.js ۪ ۪ $ npx @ruyadorno/create-100-workshop —step=6

Slide 31

Slide 31 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 32

Slide 32 text

Step 7 Let’s add tests for the output.js module The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 33

Slide 33 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ./test/output.js ۪ ۪ $ npx @ruyadorno/create-100-workshop —step=7

Slide 34

Slide 34 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 35

Slide 35 text

Step 8 Let’s test retrieve-nodejs-versions.js The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 36

Slide 36 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ./test/retrieve-nodejs-versions.js ۪ ۪ $ npx @ruyadorno/create-100-workshop —step=8

Slide 37

Slide 37 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 38

Slide 38 text

Step 9 Let’s add the missing test to the index.js module The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 39

Slide 39 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ./test/retrieve-nodejs-versions.js ۪ ۪ $ npx @ruyadorno/create-100-workshop —step=9

Slide 40

Slide 40 text

The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 41

Slide 41 text

Awesome Job! The road to 100% test coverage NodeConf EU 2023 ruyadorno.com 👏

Slide 42

Slide 42 text

Less than 100% If you can’t delete it, you should probably write tests for it so that you can maybe know what it does It means there are entire components of your application without tests What kind of program needs no test at all? The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 43

Slide 43 text

Less than 100% Would you rely on a module with no tests? A 95% test-covered program is two parts: the covered part and the uncovered part The uncovered part is a module with no tests The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 44

Slide 44 text

Bene fi ts Testable code === maintainable code Full coverage forces you to look in dark corners Investigation process tends to uncover bugs Let you focus on what you’re good at while the computer keeps track of everything else you’ve already checked The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 45

Slide 45 text

Human aspect Going from 98% to 96% looks and sounds ok Going from 100% to 99.5% is a huge loss! It feels great to see all these rows of 100% covered modules It’s addictive 😁 and will trick you into being a better developer! The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 46

Slide 46 text

Extra costs Full coverage adds up-front to development cost In return it reduces the incidence of bugs, and the costs of changing a program Less technical debt! The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 47

Slide 47 text

Extra costs Full test coverage will not prevent all bugs, what it does is gives you the power to fix these bugs more efficiently What about TypeScript and eslint preventing most errors already? Soon after you wrote types you no long knows what the code is actually supposed to do The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 48

Slide 48 text

Bad tests People will write bad tests with less than 100% coverage too The solution is to write better tests, and more easily tested components, not to accept untested code Only testing the happy path is the equivalent of “works on my machine” The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 49

Slide 49 text

Levelling Up Limit coverage values to only account for a test written for a specific module Avoiding accidental coverage, e.g: a base class method from module A gets coverage when run from module B tests tap supports it out-of-the-box with its coverage-map config option The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 50

Slide 50 text

Techniques Tests with long assertion output that have to be updated on every change? Use snapshots for that, save them in git, review the diff. Have to mock the whole world to hit a buried path? Refactor it into a discrete submodule, and only mock that module once. You’re now writing better code. The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 51

Slide 51 text

Techniques Full coverage isn’t about writing code with more tests, it’s about writing code that is more testable. Monkey-patching and other “bad” software development practices are usually totally fine in tests! The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 52

Slide 52 text

Techniques Things that really cannot be tested for some reason? Use ignore comments. This is an explicit (and well-documented, it’s going to live there with your code) way of opting-out of coverage for a very valid reason. The road to 100% test coverage NodeConf EU 2023 ruyadorno.com

Slide 53

Slide 53 text

End of the workshop Questions? Consultation time. The road to 100% test coverage NodeConf EU 2023 ruyadorno.com ⏰

Slide 54

Slide 54 text

The road to 100% test coverage Isaac Z. Schlueter node-tap author & author of the original “99% is not enough” talk this workshop is based from. NodeConf EU 2023 ruyadorno.com Special thanks to:

Slide 55

Slide 55 text

The road to 100% test coverage Ruy Adorno Linkedin: /in/ruyadorno @ruyadorno @[email protected] @ruyadorno.bsky.social NodeConf EU 2023 ruyadorno.com Thanks! Find me at: