Tool for Integration Testing in NestJS-based Backend using Mongodb Replica Set
Tags: mongodb, nestjs, backend
Published: 2024-01-15 02:47:17

Backend services built on top of NestJS framework is able to utilize the test framework Jest. NestJS and Jest come up with solutions to develop unit tests and end-to-end tests. While both are necessary, they are out of scope of this document. Here we present a documentation for integration tests, which is where the tests are run against a service integrated with its dependencies, e.g. database, cloud features, etc.

## Situation

Previously tests in my project were done manually by QA or Product Ops. For simple cases, this practice is enough. However, the project is growing and now has advance features for business process. Advance features are hard to be tested by detail in manual test, because it is exhausting and prone to human error. Furthermore, QA or Product Ops are handed with other daily tasks that takes time.

Another problem is coming from service dependencies which have advance features. For example, Mongo DB with its Node.js library called mongoose has a "Transaction" feature that is really important for data consistency. Me and my team have experienced data inconsistency in the mid of 2022. QA and Product Ops found the tests are passed in manual test. However, in production inconsistency is still exist.

## Task

Manual test can't be used in advance engineering problem. So, test automation is a way to brake this hard issue. Problems coming from dependencies can't be tested in unit test, yet it is overkill to be tested in end-to-end test. That's why engineer offer a solution to create integration test.

## Action

Engineer prepare requirements and steps to do integration test in backend services.

## Requirements

1. Shell scripts containing mongodb replica set initiation placed in root with directory `scripts/setup.sh`. Here pre-defined script for up to 3 replica set with 1 primary.
{% embed https://gist.github.com/farizmamad/a47467fb49ecbea0be56705b238220ef.js %}

2. Docker-compose file placed in root, `docker-compose.yml`. This file will create mongodb replica set containers and one db setup container.
{% embed https://gist.github.com/farizmamad/f9d0d181f79323c35b157578f8e47efa %}

3. Npm scripts for pretest, test, and posttest in `package.json`.
{% embed https://gist.github.com/farizmamad/ec3995c68b89d3e35489645297505069 %}

4. Create a file inside test directory, `test/jest-integration.json`, to enable jest identify test cases for integration test.
{% embed https://gist.github.com/farizmamad/aa8f6ebdd19ca8b89b8b750cc2c0a44a %}

5. `.env` file placed at the root of the project containing the DB_URI_TEST

```
DB_URI_TEST=mongodb://localhost:27017/db-test?readPreference=primary&directConnection=true
```

6. Create test cases inside file name with pattern `<any case>.integration.spec.ts` in test directory. For example:
```
test/order.integration.spec.ts
test/product.integration.spec.ts 
```
Make sure to connect to the primary host before all test cases.
{% embed https://gist.github.com/farizmamad/5456f63b5de80a5b2a309ecb9022c121 %}

## Steps to run the tests

1. Open a terminal at root project directory.
2. Run test command, `npm run test:integration`. Note that we don't need to call pretest and posttest commands. NestJS will call pretest before the test, and call posttest after all test cases are passed.
3. Npm will first run pretest script.
4. Wait the tests until it stops.
5. The test is done.

Note:

- if tests are passed, npm will run posttest script.
- else, test will be stopped without running the posttest. It allows us to fix the bug and then rerun the test command.