3 Minutes with Kent

How to approach writing tests

01.25.2021 - By Kent C. DoddsPlay

Download our free app to listen on your phone

Download on the App StoreGet it on Google Play

Hey there friends. So today I'm going to talk about testing and the users

of your code and how you should think about like how what do I test? So

pretty common question. And the answer is basically you take a unit of code

that you want to have tested that could be a single function or it could be

an entire application.

It really just comes down to what you want to get confidence in. Most of us

are probably in the camp of like I just want my to be sure that my entire

application works and that's great. And that'sTypically where where I'm

thinking and in my own mindset sometimes though, it's hard to accomplish

the thing that you're trying to test from a whole application perspective

and that's when you go to lower level forms of testing but that's not what

I'm going to talk about today.

So once you've identified the unit of code that you want to have tested the

next thing you need to do is think about who the users of that code are. So

if we're talking about a react component, for example, then you're going to

have two users of your code.

And in fact most of the time,You're going to have two users of your code,

unless you're doing full-on and to end testing with nothing mocked. And the

two users are the end user who's going to be interacting with that code and

the developer user who's going to be making network requests to your API

handler or maybe they're calling into your function or their rendering your

component, whatever it is.

So you have those two two users of your code. And so then as you're

thinking about what you know, what you should write.For your test you're

going to say okay well so what is the what do mind users do What are the

use cases that this code supports and how do I make like what are of those

use cases what are the most important things that I ensured don't break?

Because they're like for an application there are some parts of the

application that matter more than others. And so you're going to identify

all the use cases that you want to support and kind of sort them in your

mind or maybe even write it down. The order of priority for those things.

So once you have decided what use cases you want to support you just go

one.By one to write a test for that particular use case. And the way that

you write that is you could even write this in code comments or something

before you write the test you just say how do if I were a user manually

testing this whether you're the end user or the developer user probably

both how would I interact with this component to make it do the thing that

I'm trying to support?

How do I show that use case in my test? And then you write code to do that.

I hope that's helpful and I hope you have a splendid day. Good luck.And

writing those tests get yourself a lot of confidence and we'll see you

around.

More episodes from 3 Minutes with Kent