API with NestJS #96. Running unit tests with CI/CD and GitHub Actions

Docker NestJS

This entry is part 96 of 158 in the API with NestJS

We should closely monitor the state of tests in our NestJS application. In a few previous parts of this series, we’ve used Docker and GitHub actions to design a CI/CD pipeline that automates our deployments. In this article, we avoid deploying faulty code by running unit tests before each pull request automatically.

Preparing the Docker image

The first step is ensuring that our tests can run in our Docker image. To verify that, let’s look at the we’ve built so far.


If you want to see how we’ve built the above configuration, check out the following articles:

We would fall into a significant pitfall if we used the above Docker image to run tests.

To install the dependencies, we run . Adding the flag tells NPM to skip installing all of the libraries listed in . However, this is typically where we list libraries such as Jest that we need for testing.


Let’s modify our configuration to accommodate that.


Since we need the packages listed in to run our unit tests, we install all of them during the stage by skipping the flag.

This would make the resulting Docker image heavier if not for a modification to the stage. By adding the command at the bottom, we eliminate the libraries from before making the final Docker image.

Thanks to doing all of the above, the result of the stage in the has all of the dependencies, but they are stripped when no longer necessary.

We also add to ensure the latest version of NPM and avoid warnings in the terminal.

Running tests locally

We must run our tests with the Docker image targeting the stage before removing the . Therefore, let’s use the flag to build the image without running the last stage from our .

When we look at the stage in our , we can see that it doesn’t have the keyword. When running the image, we must provide Docker with the desired command. Let’s run inside the shell of a Docker container running with our image.

PASS src/authentication/authentication.service.test.ts
The AuthenticationService
when creating a cookie
✓ should return a string (17 ms)

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

Running Prettier

When checking the formatting, we usually set up Prettier using the flag to overwrite our files.

Instead, we want to be notified when our code does not meet the formatting standards. To do that, we need the flag.

If a particular file is not formatted correctly when running the above command, Prettier lists it. It also uses the exit code to signify an error.

Let’s rebuild our Docker image to include the above command.

Now, we can run Prettier in our Docker container to verify our code.

Running ESLint

The official NestJS starter repository comes with ESLint configured and ready to go. We can easily use it with our existing configuration.

Running the tests with GitHub Actions

In the previous part of this series, we created a GitHub Actions workflow that runs every time we push our change to the master branch. Before that happens, we usually make a pull request to merge our changes. Let’s create a new GitHub Actions workflow that runs every time a PR is created.


Thanks to adding above, there is a set of steps happening above every time we create a pull request:

  1. For GitHub Actions to access our code, we need to check out our repository. To do that, we use the action provided by GitHub.
  2. We build the Docker image targeting the stage.
  3. Since our environment is ready, we run Prettier, ESLint, and unit tests.

Let’s create a Pull Request and verify that everything works as expected.

We can see the information saying that “all checks have passed“. When we click “Details”, we can see more job information.

Protecting our master branch

Even though we have the tests in place, nothing stops us from disregarding them and merging the changes even if they fail. Let’s change that.

To do that, we need to open our GitHub repository and go to Settings -> Branches. Here, we can add a branch protection rule prohibiting merging changes if our status checks haven’t succeeded.



In this article, we established a process of testing our application whenever we want to merge new changes to the master branch. To do that, we had to modify our Docker image slightly to install all necessary dependencies and remove them when they were no longer needed. We’ve also created a new GitHub Actions workflow to run tests on every pull request.

Thanks to the above, we couldn’t merge our changes if any of our tests failed. This gives us an additional layer of security and prevents us from deploying a faulty version of our application.

Series Navigation<< API with NestJS #95. CI/CD with Amazon ECS and GitHub ActionsAPI with NestJS #97. Introduction to managing logs with Amazon CloudWatch >>
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
1 year ago

You are amazing bro! Thanks for your awesome work.

Ngoc Anh
Ngoc Anh
1 year ago

Can you make a tutorial on how to export files with excel, csv, pdf and import data files? I have no idea to implement with Nestjs. Thanks Bro