Testing
We will implement End-to-End testing with Jest to test our API. We will test the email tokens as well, we will need those tokens to be able to test the API completely. For the testing we will use:
-
Jest: Testing package
-
nodemailer-mock: Testing email
-
supertest: Testing the Express API endpoints
-
cheerio: Parsing the email HTML and getting the tokens
Let's install and set them up
Installing dependencies
Setup and configurations
Jest Configurations
Create a file in the root directory named jest.config.js
create two folders in src
; one for nodemail-mock
named __mocks__
, and one for the test files __tests__
Test API Status
Inside __tests__
create a file named api.test.ts
. In this file we will create a jest suit to test the API status and check if our server is working.
Testing Auth System
This test suite is large, we will go through it and explain it bit by bit, and I will link the file link to GitHub at the end. Before we start, Let's create the mail mock and some other functions we will use.
Mail mock
inside __mocks__
create a file named nodemailer.ts
.
This will mock the nodemailer and the emails we send in the API.
Email Token reader
This function will parse the Email HTML and get us the token. Create a file inside utils
named mailerUtils.ts
Truncate DB
After testing, we will empty the DB. That's the purpose of this function; to truncate all the tables. Now, create a file inside utils
named truncateDB.ts
Note: this function only works for Postgres.
Now we are all set to start working on auth.test.ts
. Create this file in __tests__
. We create a test suite for the Auth system. And after the suite has finished, we truncate the DB.
We start by creating the initial user payload. I created it outside the test functions to make global scope for all the suit. We will add the tokens to it and use them across the tests inside this suite.
In this test suite, we have 23 test. I tried to test all the use cases as possible, And that's the role of testing.
Now, we have the skeleton of the Auth Test, Let's add tests one by one. Starting by creating a user.
Test Creating a user
We have 3 tests for creating a user. Simple and correct one, with email conflict, and validating user email.
Test Create User:
We can move the email testing to another function, and call it. Create a function outside the scope of the test suite:
Now update the test:
Test Create user with email conflict:
Test Validate user email:
Test Login
Testing Refresh Token
Testing forgetting password
Note:mock.getSentMail()
returns all the emails send in this instance.
Testing Reset password
Test Update password
Testing update password with email confirmation:
You will find all these functions in one file here
Run testing
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722177123567/694bb15a-b8cc-408f-888f-b2c7183be12b.png align="center")
All tests passed successfully!
Postman Collection
We created the tests for our auth system. And we can use Postman to test even further. I created a postman collection to test these APIs. I might deploy the API and add the endpoint to this Postman's collection. So you can test the API online as a DEMO.
https://documenter.getpostman.com/view/13363083/2sA3kYjLAb
Summary
This article was a rich documentation of creating a real API, this API is a part of a real project I'm working on. We can add more features to this API. Such as MFA, 2FA, SMS and OTPs, Handlebars for beautiful HTML Email UIs... We might work on them in another article in the future. Following the Kaizen approach, we can add small improvement in each step.
See you in the next one 💪