Check out a free preview of the full API Design in Node.js, v4 course:
The "Unit Testing Overview" Lesson is part of the full, API Design in Node.js, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Scott describes unit tests as testing individual pieces of logic independently. In order for unit tests to be effective, the code should be written in a testable way. For example, function arguments are easier to test than closure variables.

Get Unlimited Access Now

Transcript from the "Unit Testing Overview" Lesson

[00:00:00]
>> We're gonna write some tests. The thing that everyone loves doing is writing tests. I promise you it's actually really cool and easy with Express especially the tools now. So some of the tools that we're gonna be using to write some tests include Jest, ever heard of Jest?

[00:00:15] It's basically a ripoff of Mocha and Jasmine but does a really good job honestly. And Supertest, we'll be using that too. We'll talk about those two different things and why we're gonna use them. In the meantime let's install some things, let's grab this and install, and I'll talk about what these things are.

[00:00:35] So Supertest is a tool that's gonna allow us to do what's called integration testing. I'll talk about what that is. And then we just download the TypeScript types for it. Jest is a testing framework just like Express is a server framework, just like React is a client framework, and allow us to write some tests and the types for that.

[00:00:56] And then ts-jest allows us to write our Jest and TypeScript Okay, so we got all those things. The next thing we wanna do is we want to run npx ts-jest config:init. What this is gonna do is create a configuration file on our project for Jest. We don't even have to touch it.

[00:01:19] We just need it there. You can leave it alone. Okay, so different types of tests. We have a unit test. Anybody here wanna describe unit test? Don't look at my stuff. Just talk about it, don't look at my definition. I know people here have written unit tests. I know you've written unit tests.

[00:01:44] What's a unit test?
>> I mean, in the unit test we're just testing the unit of whatever you're writing. Suppose, if you are contacting to the database you are not testing the code which getting the data from database, or testing the code only, what you are doing after getting the record from database and then you're running some logic.

[00:02:03] So, writing tests for your unit of code.
>> Yeah, well said I don't think I could have said it better myself. Pretty much exactly what you said it's just testing the individual logic bits isolated from everything else. Not in like conjunction and how they all work together, but how does this one function work?

[00:02:25] Let me test that. You know like, and nothing else. And even then within that function it's like, well, I just wanna test that one branch within that function, or I wanna test what happens if I do just this one argument. Like it's just testing individual things in isolation versus testing systems and abstractions because then you started getting into integration testing, end to end testing all different types of testing.

[00:02:47] So that's what unit test is. So let's look at an example of what, in order to write unit tests you need to have testable code. So when you're writing your code you have to be thinking about can I even test this? Is this testable? And that's something that you should always be asking yourself.

[00:03:06] One of the things I look at a lot when I'm like reviewing people people's code is how are you going to test this? This is brilliant, like I don't know how you did this, this is amazing, but how are you gonna test this? How do we keep it brilliant, how do we keep it amazing?

[00:03:18] Because it is actually untestable as you put the whole app in one function. So [LAUGH] I don't know what we're supposed to do here, okay? So let's look at an example of not testable code. So we wanna write a test for this action function. We can, but even with only three or four lines of code this is actually really hard to test.

[00:03:36] I'll tell you why. One, it's not exported so I don't know how we'll even get a reference to it somehow. Two, it's literally creating a closure with this variable outside of here. So we're not actually exporting this function, so we can't really test it. The second thing is it's creating a closure by reaching out and grabbing this variable in another scope.

[00:04:01] So if we wanted to export this function into another file, well it has this extra dependency that we have no access to, that's a closure over here. Good luck mocking that out, good luck changing that. You're just making it really hard. So that's not really testable. But it looks so innocent.

[00:04:16] It looks like we mean it was really quick little function, why is this not testable? Well, the testable version will look something like this. It's exported, so we can import it by itself. And instead of reaching out and grabbing something or creating a closure, it declares it as an argument and it can be parsed in.

[00:04:30] This is great because in our tests we can pass in different values here to see what the result would be, which is amazing that way we can test the behavior, we can try to break the function, we can see what happens under different conditions. We can never do that here.

[00:04:44] How would you change the value of value inside your tests? I don't know how, I don't know how you would do it. It's actually impossible [LAUGH] without some type of hack. So you would have to write the test in the same file as its function. I would imagine it'd be really weird.

[00:04:58] I don't know how you do it. But here it's not a problem. Okay, this function is not bound to the file that it's in whereas this function is pretty much bound to this value. It cannot move without functioning differently