API Design in Node.js, v4

Integration Testing with SuperTest

Scott Moss

Scott Moss

Superfilter AI
API Design in Node.js, v4

Check out a free preview of the full API Design in Node.js, v4 course

The "Integration Testing with SuperTest" 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 demonstrates how integration tests can be used to test an entire route's functionality. The supertest module provides methods for building requests and validating the result of those requests.

Preview
Close

Transcript from the "Integration Testing with SuperTest" Lesson

[00:00:00]
>> Okay, let's talk about integration testing. So integration testing, unlike a unit test, integration test will test how an entire route works by actually making a request like a client would, and then just observing what the API sent back and making assertions on that result. So we can actually just use jest plus supertest to run integration test.

[00:00:23]
So here's an example of what that will look like. And obviously, I'm gonna walk you through how to do it. So I'm gonna go back to our code. I'm going to put something on the root of the source folder. I'm gonna make a new folder called test again.

[00:00:39]
And I'm gonna test, I don't know, routes.test.tsr. There we go. In order to do integration tests, there's a lot of ways you can do it. You can spin up the server yourself and then just make an API call to the server. Or we can just import the express app and supertest will pretty much do all that stuff for us in memory.

[00:01:07]
So I'll just import our app. So I'll say import app from, I believe it's in the server, that's our app. Then I'm gonna import supertest from supertest Then we'll write some tests, so I'm gonna say describe, let's test out the, Let's test out this route right here where you get something you just do get, let's test that out.

[00:01:40]
Obviously, gonna change it so doesn't break but we'll do that in a minute. So we'll describe a GET to index like that. And then I'll say it should send back some data. And this is gonna be async, it blocks can be async, nd I'm gonna say results equals await.

[00:02:12]
I need to make a request. So the way you can make a request or I guess, yeah, I forgot to change the syntax. So you can just say like supertest (app) and then you can start doing things like changing the headers, sending up the body, all different things you would wanna do.

[00:02:31]
So in the case of like a get request, we don't really have, for instance, we're not gonna be sending up anything here. We're not gonna be sending any headers. But we do wanna make a get request. So what we'll do is we'll say, .get request to slash like that.

[00:02:47]
And then what we can say is okay, I expect res.body.message toBe hello, I could do that. So what I'm gonna do is go back to our server, I'm gonna say res.json{message: 'hello' like that. And then now I'm gonna run this test. Okay, I still got the other one [INAUDIBLE], let's not to the other one.

[00:03:26]
Let me get the other one to pass, so it's not in the way. There we go, okay, let's run the test. Okay, so now you can see the second one ran and it passed because that's exactly what the server sends back on that request. And you can see right there, Morgan's logging, Morgan ran a log, Morgan which is our middleware that we wrote, is actually logging that something did a GET request to slash got to 200.

[00:03:52]
So it did hit, it ran the handler and everything like a request would. It's gonna run through all of it. And that passed and just to be sure, we can change that to something else. And we will get something else, expected, hello, instead we got back nope. Easy as pie.

[00:04:21]
Any questions on integration testing? Nope? Okay, everybody's good at testing at this point? [LAUGH] yeah.
>> [INAUDIBLE] know that's the Frontend Masters website has a topic on testing, if you want to learn more just about testing in JavaScript, there's a testing topic.
>> Yeah, that is actually true.

[00:04:53]
There's really good testing, there's a lot of good testing stuff in Frontend Masters in general, but yeah, there's a testing course in JavaScript that is pretty effective, that goes through way more stuff than I'm talking about here. But for the most part on the back end, I'm mostly just doing unit testing, integration testing.

[00:05:11]
Yeah, Frontend is a little more difficult, snapshotting, end to end, unit. I heard someone else do this other, diffing, I'm like, man, this is so many different tests on the Frontend, but Backend's pretty simple.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now