DEV Community

Cover image for Testing NodeJs/Express API with Jest and Supertest
Chinedu Orie
Chinedu Orie

Posted on • Updated on

Supertest Jest Testing NodeJs/Express API with Jest and Supertest

This is part three of building API using Express, Sequelize, and Postgres. In part two, we built simple API endpoints to demonstrate CRUD operations in Sequelize. In this article, we'll be focusing on writing end-to-end tests for the API endpoints created in part two.

Explanation of terms

  • End-to-end testing - a type of test which tests that the flow of an application from start to finish works as expected. This is also known as functional testing. An example of this type of test is testing an endpoint or a route which involves testing everything needed for the endpoint to work such as database connection, dependencies, etc.

  • Test Runner - a library or tool which picks up source code (tests) in a given directory or file, executes the test and write the result to the console or any specified location, example Jest, Mocha.

  • Jest - Jest is a JavaScript testing framework developed by Facebook. It works out of the box with minimal configuration and has in-built test runner, assertion library and mocking support.

  • Supertest - A library for testing Node.js HTTP servers. It enables us to programmatically send HTTP requests such as GET, POST, PATCH, PUT, DELETE to HTTP servers and get results.

Now that we have explained the basic terms, let us dive deep into the main business.
If you've been following along from the previous articles, then open it in your favorite text editor, else clone the repository used here.

Step 1 - Install Jest and supertest

Open your terminal and cd to the project root directory and run the command below:

npm install --save-dev jest supertest
Enter fullscreen mode Exit fullscreen mode

Step 2 - Configure Jest

Open the package.json and add the code below to it.

 "jest": {
    "testEnvironment": "node",
    "coveragePathIgnorePatterns": [
Enter fullscreen mode Exit fullscreen mode

That is the basic configuration that we need to get jest set for testing our API. Any file that you want jest to ignore is placed inside the "coveragePathIgnorePatterns". "coveragePathIgnorePatterns" specifies a regex that matches the directory to be excluded, in our case we want it to ignore node_modules directories.

Next up we add the test script. Inside the scripts portion of the package.json, add the script below:

"test": "jest"
Enter fullscreen mode Exit fullscreen mode

Step 3 - Test Jest configuration

Now, let us confirm that jest is all set to run our test. In the terminal run npm test. You will notice an error as shown below printed on the console, this is an indication that jest is set up.

Jest error when no test is specified

Let us add a simple test to verify the configuration. Create a new directory called tests and add a new file sample.test.js. Inside the sample.test.js, add the code below:

describe('Sample Test', () => {
  it('should test that true === true', () => {
Enter fullscreen mode Exit fullscreen mode

Now, run npm test, you'd get an output as shown below:
Sample test output

How does Jest recognize a test file?

Jest recognizes test file in three ways:

  • files that have extension .test.js
  • files that have extension .spec.js
  • All files inside __tests__ folder or directory.

Testing the API Endpoints

Now that we have got the test environment set up, it is time to start testing the API endpoints. Since our endpoint needs to make a request to the database, we need to set up a test database. The reason for setting up a test database is that we'll be dropping the database each time we run a test. Dropping the database each time the test is run ensures the integrity of the test. That is, if a test is about creating a post record in the database, we want to be sure that there was no post record in the database before the test was run, that way, we are sure of the result got from the test.

Step 4 - Create a test database
In the part one of this article, we created two databases, one for development and the other for testing. Follow the link to create a test database if you have not done so.

Step 5 - Configure test scripts

We need the following scripts:

  • pretest - The pretest is an npm script that is automatically invoked when the npm test command is invoked. We'll hook in the command to change the environment to test and refresh the database before each test runs.

  • migrate:reset: This command will be responsible for refreshing the database before each test runs.

Now edit the scripts for package.json as shown below:

"scripts": {
    "start-dev": "nodemon index.js",
    "migrate": "npx sequelize-cli db:migrate",
    "migrate:reset": "npx sequelize-cli db:migrate:undo:all && npm run migrate",
     "test": "cross-env NODE_ENV=test jest --testTimeout=10000",
    "pretest": "cross-env NODE_ENV=test npm run migrate:reset"
Enter fullscreen mode Exit fullscreen mode

What to note from the modification of the script:

  • cross-env - an operating system agnostic package for setting environment variables. We used it to set the NODE_ENV to test so that our test can use the test database. Run the command below to install cross-env.
npm i -D cross-env
Enter fullscreen mode Exit fullscreen mode
  • --testTimeout flag - This increases the default timeout of Jest which is 5000ms. This is important since the test runner needs to refresh the database before running the test.

Step 6 - Test the scripts

npm test
Enter fullscreen mode Exit fullscreen mode

If everything is okay, you should see the output below on the terminal:

Alt Text
Looking closely at the screenshot above, you'd notice a line using environment "test" which shows that cross-env has changed the NODE_ENV.

Final Step - testing the routes/endpoints

Now, let's begin writing tests for the endpoints. Create a file named routes.test.js inside the tests directory

touch tests/routes.test.js
Enter fullscreen mode Exit fullscreen mode
  • Testing Create Post Endpoint

Copy the following code into tests/routes.test.js:

const request = require('supertest')
const app = require('../server')
describe('Post Endpoints', () => {
  it('should create a new post', async () => {
    const res = await request(app)
        userId: 1,
        title: 'test is cool',
Enter fullscreen mode Exit fullscreen mode
  • The describe function is used for grouping together related tests
  • The it is an alias of test function which runs the actual test.
  • The expect function tests a value using a set of matcher functions.

visit the Jest docs for a full list and details of jest functions.

Now, run the test

npm test
Enter fullscreen mode Exit fullscreen mode

The output is shown below:

Alt Text

For the complete code for the tests for all the endpoints, check the repository.


We have been able to walk through the process of writing tests for API endpoints that interacts with the database. In the last part of this article, I'll be writing on integrating CI/CD and code coverage tools to the test environment. Until then stay tuned.

Feel free to reach out to me if you have any questions or suggestions on making the article better. You can also share your thoughts via the comment section below. Thanks!

This article was originally published in my blog

Top comments (30)

avinash8686 profile image

There are no cases for api failure, It would be great if u add the failure cases as well, overall the post was pretty good and easy to understand.

hotdangca profile image
James Robert Perih

You could:

Enter fullscreen mode Exit fullscreen mode

when you do something intentionally wrong, as well as:

expect(res.body).toEqual('Post not found');
Enter fullscreen mode Exit fullscreen mode

Various other uses of expect here

nedsoft profile image
Chinedu Orie

Yeah, I've included some failure test cases in the sample project, I'd update the article to reflect it. Thanks for the hints.

nedsoft profile image
Chinedu Orie

Yeah, that's true, I seemed to focus on the success side. I'll update it to take into account your feedback. Thank you.

devorji profile image
Chigbogu Orji

I tried using jest after I first read your article, but as I changed from Windows7 to Windows10 after Microsoft stopped its update(windows7); my jest test ain't working anymore.

I got the error app.address is not a function
What could be the cause, am using yarn as my package manager.

I have being using mocha and chai for my tests and it works, none works these days
Thanks in anticipation.

nedsoft profile image
Chinedu Orie

Could you check if the discussion in this thread helps

prashant1k99 profile image
Prashant Singh

Thanks for the article, I followed the instructions and wrote the test code. For anyone who is looking for the sample Express Route Test using Jest (As explained in the above article) can visit this sample GitHub Repo I created.
Repo Link:

mememe profile image

This is good content. I hope you get to complete the whole CRUD testing.

martinnuc profile image
Martin Nuc

It looks like the database is not refreshed between endpoint tests, right? Means endpoint tests could affect each other, right?

nedsoft profile image
Chinedu Orie

Yes, that is definitely a short-cut solution, you could make each test suite independent of each other

elishaking profile image
King Elisha

Great article

yassern profile image
Yasser Nascimento

Nice! Thanks a lot :)

adilsoncarvalho profile image
Adilson Carvalho

Hello Chinedu,

It's 2021 and your post still helping people. I am one of them.

Cheers mate!

brunolnetto profile image
Bruno Peixoto

I receive a timeout with log ": Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.Error:

isaackomeza profile image
Isaac Komezusenge

Good article with rich content!! Thank you Orie

akopchinskiy profile image

What about more real-world complex example with database connectors and sharing them around several test suites with Jest? )))

gabrielbb profile image
Gabriel Basilio Brito • Edited

Step 2 is not needed anymore. Is the default behavior. Except the test script of course

gennady77 profile image

Links in the Step4 doesn't work

hidayahweb profile image
Hidayah Ramadlana (ID)

your website is not accessible

warrenwongcodes profile image
Warren Wong

The two links to how to setup database are erroring out.