Subscribe
Unit Testing Dockerized Node.js Applications with Jest
5 mins read

By: vishwesh

Unit Testing Dockerized Node.js Applications with Jest

Testing is an essential part of developing any software application, including Node.js applications. In this article, we will explore how to unit test a Dockerized Node.js application using the Jest testing framework.

What is Unit Testing?

Unit testing is a software testing method in which individual units or components of a software application are tested in isolation from the rest of the application. The goal of unit testing is to verify that each unit or component of the application works as expected.

Why Unit Test Dockerized Node.js Applications?

Docker is a popular containerization technology that allows developers to package applications and their dependencies into lightweight containers. Dockerizing a Node.js application makes it more portable and easier to deploy. However, unit testing Dockerized Node.js applications can be challenging, as the application is running inside a container.

Unit testing Dockerized Node.js applications is important because it helps ensure that the application is working correctly before deploying it to production. Unit tests also help identify and fix bugs early in the development process, which saves time and reduces the risk of introducing bugs into the application.

Setting Up a Dockerized Node.js Application

To follow along with this tutorial, you will need to have Node.js and Docker installed on your computer. You can download Node.js from the official Node.js website (https://nodejs.org/) and Docker from the official Docker website (https://www.docker.com/).

We will start by creating a simple Node.js application that we will later Dockerize and unit test. Create a new directory and navigate to it in your terminal. Run the following command to initialize a new Node.js project:

npm init

Follow the prompts to initialize the project. Once the project is initialized, create a new file named app.js. This will be the main file for our application. Add the following code to the app.js file:

function sum(a, b) {
  return a + b;
}

module.exports = sum;

This is a simple function that takes two arguments and returns their sum. We will use this function to demonstrate unit testing.

Create a new file named test.js. This will be our unit test file. Add the following code to the test.js file:

const sum = require('./app');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

This is a simple unit test that tests the sum function we created earlier. It expects the sum of 1 and 2 to be 3.

Run the following command to install the Jest testing framework:

npm install jest --save-dev

This will install the Jest testing framework as a development dependency.

Now, we are ready to run our unit tests. Run the following command:

npm test

This will run all the tests in the test.js file. You should see the following output:

PASS  ./test.js
  ✓ adds 1 + 2 to equal 3 (3 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.642 s
Ran all test suites.

Congratulations! You have successfully set up a Node.js application and written a unit test for it using the Jest testing framework.

Dockerizing the Node.js Application

Now that we have a working Node.js application and a unit test, we can Dockerize the application. Dockerizing a Node.js application involves creating a Docker image that contains the application code and its dependencies.

Create a new file named Dockerfile in the root directory of your project. Add the following code to the Dockerfile:

# Use an official Node.js runtime as a parent image
FROM node:14-alpine

# Set the working directory in the container to /app
WORKDIR /app

# Copy the package.json and package-lock.json files to the container
COPY package*.json ./

# Install the dependencies in the container
RUN npm install

# Copy the rest of the application files to the container
COPY . .

# Set the command to run the application
CMD ["npm", "start"]

This Dockerfile sets up a Node.js runtime environment, installs the application dependencies, and sets the command to run the application.

To build the Docker image, run the following command:

docker build -t my-node-app .

This will build a Docker image named my-node-app.

To run the Docker image, run the following command:

docker run -p 3000:3000 my-node-app

This will start the Docker container and expose port 3000.

Unit Testing Dockerized Node.js Applications

Now that we have Dockerized our Node.js application, we can unit test it using the Jest testing framework. To run the unit tests inside the Docker container, we need to modify the Dockerfile.

Add the following code to the Dockerfile:

# Use an official Node.js runtime as a parent image
FROM node:14-alpine

# Set the working directory in the container to /app
WORKDIR /app

# Copy the package.json and package-lock.json files to the container
COPY package*.json ./

# Install the dependencies in the container
RUN npm install

# Copy the rest of the application files to the container
COPY . .

# Run the unit tests in the container
CMD ["npm", "test"]

This Dockerfile sets up the Node.js runtime environment, installs the application dependencies, copies the application files to the container, and runs the unit tests inside the container.

To build the Docker image with the unit tests, run the following command:

docker build -t my-node-app-test .

This will build a Docker image named my-node-app-test with the unit tests.

To run the unit tests inside the Docker container, run the following command:

docker run my-node-app-test

This will run the unit tests inside the Docker container and display the test results in the console.

Congratulations! You have successfully Dockerized a Node.js application and unit tested it using the Jest testing framework inside a Docker container.

Conclusion

Unit testing is an essential part of developing any software application, including Dockerized Node.js applications. In this article, we explored how to unit test a Dockerized Node.js application using the Jest testing framework. We also learned how to Dockerize a Node.js application and run the unit tests inside a Docker container. By following these best practices, you can ensure that your Dockerized Node.js applications are working correctly before deploying them to production.

Recent posts

Don't miss the latest trends

    Popular Posts

    Popular Categories