Test HTTP endpoint

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Pain Free Mocking with Jest course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Pain Free Mocking with Jest, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Pain Free Mocking with Jest
  • [00:00 - 00:13] In this lesson, we'll dive into testing HTTP endpoints using SuperTest. It's powerful to the simplifies making requests against your application, and asserting on the response.

    [00:14 - 00:22] We'll focus on testing the signup endpoints as an example. To begin, let's install SuperTest as a dev dependency.

    [00:23 - 00:40] Open your terminal and run this command. [silence] Next, let's review the signup endpoints that we want to test.

    [00:41 - 00:54] It's important to have a clear understanding of the endpoints functionality and expected behavior before writing our tests. I made the change to the MongoDB URI.

    [00:55 - 01:05] And corrected the port to 27017. That is for test.env, the .env file, and the dev.env files.

    [01:06 - 01:17] I added more changes to our app.js file. I created a router, the auth router, and I added it as a B2F function to this route, API V1 auth.

    [01:18 - 01:38] And in the auth router, we have to route the root path for signup and the login route for login. You have already seen the signup controller before.

    [01:39 - 01:57] It accepts user object in the request body and persists that to the database using the create user function that we created earlier also. Now, let's create our test for the signup endpoints using SuperTest.

    [01:58 - 02:07] We started by creating a new describe block to encapsulate our test suite. And it is titled signup routes.

    [02:08 - 02:18] Denoting the system on the test. Within our describe block, we defined different test cases.

    [02:19 - 02:28] And we also have the before all and after all hooks. We want to ensure we have a clean slate for each test.

    [02:29 - 02:44] And in the before all hook, we use the user model and we call delete many to clear the collection. And then after all, we close the database connection and also close the application.

    [02:45 - 02:56] Now up the JS, we exported our server and the MongoDB connection that we created here. Now, let's review our test cases.

    [02:57 - 03:08] For this test case, even a user in request body, it should return status 201. In the average step, we created our user object.

    [03:09 - 03:26] And in the assess step, we execute the request function, which is imported from SuperTest. And we supply the route that we want to test or the HTTP endpoints that we want to test.

    [03:27 - 03:38] And in the request body, we send the user and we make an assertion here using dot expect. Let's review this test case.

    [03:39 - 03:52] Given a user in request body, we expect that the signup routes will create a user in database. And in the arrange step, we created our user object.

    [03:53 - 04:12] And then in the act step, we executed our request from SuperTest against our server. And using the post method, we supplied the HTTP endpoints we want to test.

    [04:13 - 04:25] And we sent our user objects that we defined earlier. And then we run a query against the database to get all the users in the database.

    [04:26 - 04:46] We then made an assertion checking the length of the users in the database to be equal one. And to also check inside the item at the first index, made an assertion that the email of the item in the first index will be equal.

    [04:47 - 05:04] [email protected] One thing you will notice is that we are not making use of any marks or test doubles in this test suite. In integration testing, you want to run your test against all of its dependencies.

    [05:05 - 05:18] And that is why integration tests have higher availability guarantee than unit tests. Next, I want to run the application and test the signup HTTP endpoints using WestAPI client.

    [05:19 - 05:32] Our application is running on port 3000. Here is the WestAPI client extension in Visual Studio Code.

    [05:33 - 05:45] And in the address bar, I have supplied the signup HTTP endpoints as for the method I selected post. In the body of the request, it is of JSON type and here are the details.

    [05:46 - 05:54] I will click the send button to test our HTTP endpoints. And the status is 201 created.

    [05:55 - 06:09] If I click the send button again, the request will fail and it should return the response of 400. And here, in the body, we have the email taken.

    [06:10 - 06:24] Supertest is a powerful tool that simplifies testing HTTP endpoints by allowing us to make requests against our application and make accessions on the response . [ sub by sk cn2 ]