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
-
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.
-
Docker-compose file placed in root,
docker-compose.yml
. This file will create mongodb replica set containers and one db setup container.
-
Npm scripts for pretest, test, and posttest in
package.json
.
-
Create a file inside test directory,
test/jest-integration.json
, to enable jest identify test cases for integration test.
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
- 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.
Steps to run the tests
- Open a terminal at root project directory.
- 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. - Npm will first run pretest script.
- Wait the tests until it stops.
- 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.
Top comments (0)