Back to Blog

MERN Jest Testing (Part 3)

Wajiha Niazi

Jest configuration

What is unit testing?

Unit testing is a part of the software development process in which the smallest testable code block is called a unit. The purpose is to validate that each unit of the software code performs as expected. A unit may be an individual function, method, procedure, module, or object.

There are many different frameworks for testing JavaScript here we are going to use Jest for testing our doc.

Step 1: Install packages

You need to add an npm package to your project to begin writing tests, jest.

npm i jest

In the above code,

  • jest: Jest is a framework for testing JavaScript code. Unit testing is its main usage of it.

Step 2: Add test script

Open your package.jsonfile and add the test script to the scripts.

"scripts": {
    "test": "jest --runInBand --forceExit",

In the above code,

  • jest to execute test suites.
  • --runInBand Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests. This can be useful for debugging.
  • --forceExit force Jest to exit after all tests have completed running. This is useful when resources set up by test code cannot be adequately cleaned up.

Step 3: Create a configuration file for tests

create a file jest.config.js  in the root folder and add the code below

module.exports = {
  testEnvironment: "node",
  setupFiles: ["dotenv/config", "/test/config/setEnvVars.js"],
  setupFilesAfterEnv: ["/test/config/setupAfterEnv.js"],

In the above code,

There are three config options in Jest for having some code running before each test testEnvironment,setupFiles, and setupFilesAfterEnv but these have one difference:

  • testEnvironment: The test environment that will be used for testing. The default environment in Jest is a Nodejs environment. you can use another test environment instead of Nodejs.
  • setupFiles: will be executed before the test framework is installed in the environment and before executing setupFilesAfterEnv. In this option, Jest will use the file ****setEnvVars.js to update or replace the configuration.
  • setupFilesAfterEnv: will be executed after the test framework has been installed in the environment. In this option, Jest will use the file ****setAfterEnv.js to update or replace the configuration.

This is important because this is where you can switch different databases from the current database to a testing database.

Step 4: Create a test & config folder

  1. Create a test folder in your root directory.
  2. Add a config folder in the test folder.
  3. Add the setEnvVars.js line in test/config and add the code below.

process.env.MONGO_URL = "mongodb://";
process.env.PORT = 4000;

If you are using MongoDB atlas Replace MONGO_URL with the value of your URI.The values in this section will be used on the blog_app_test database because we don’t want Jest to add or remove the data from our blog_app database when we run the tests.

Add a setupAfterEnv.js file in test/config folder and add the code below.

const mongoose = require("mongoose");

const mongoManager = require("../../mongo/connection");

beforeAll(async () => {

afterAll(async () => {
  if (mongoose.connection.readyState !== 0) {
    await mongoose.disconnect();

In the above code,

  • beforeAll Runs a function before any of the tests in this file run.
  • afterAll Runs a function after all the tests in this file have been completed.
  • You'll need to connect and disconnect the database before and after all tests (because we don't require the database once testing is complete).
  • This will allow jest to use the data from test/config/setEnvVars.js to create a testing environment.
  • mongoManager Therefore we import the database connection function as a mongoManager variable.

Step 5: Start writing tests

  1. Now you can write your first unit test. For this example we are going to test a function getPage/utils/functions.js

const getPage = (currentPage, currentLimit) => {
  const page = currentPage || 1;
  const limit = currentLimit || 20;
  return Math.max(page * limit - limit, 0);

module.exports = {

This function will be used to create pagination later, but we can test it now.

  1. Create a test file since the function we are going to test is located in the utils folder. We can add also a folder named utils in the test folder in order to organize our codes well/tests/utils. Now add functions.test.js into the same folder

const { getPage } = require("../../utils/functions");

describe("Testing getPage() function", () => {
	const getPageResult = getPage(12, 4);
	it("It should return 44 when it receive getPage(12,4)", () => {

In the above code,

  • We use to describe to describe the unit test.
  • In it, we write the actual test code. Specify what the test performs in the first argument and then in the second argument, write a callback function that contains the test code. In the callback function, the test passes if the answer match, else it fails.

Share on social media: