Basic and effective NestJS Testing implementation guide
Introduction
Implementing testing in software development not only helps detect bugs early but also ensures system stability when performing upgrades or changing code. Testing helps programmers feel more confident, minimizes logic error risks, and creates a living document of how modules operate.
- In NestJS, we usually focus on three main concepts:
- Unit Test helps test each class or function independently
- End-to-End (e2e) Test tests the entire operation flow from request to response through a real server
- Test Coverage is an index measuring the percentage of source code that has been tested by test suites.
Prerequisites
In this article, I will continue to use the code from previous articles to implement tests; you can review those articles to get the source code to continue, or you can write similar tests based on the content according to your needs.
Detail
Update file package.json, focusing on the jest field
The above configuration sets up Jest to automatically find .spec.ts files in the src directory, and at the same time determines the files to collect coverage (excluding config, module, dto files). Especially, the coverageThreshold section sets a mandatory rule that the project must reach at least 80% coverage for all criteria to be considered passing the check.
Update file config cá»§a jest-e2e.json
This is a specific configuration file for End-to-End testing. It directs Jest to find files ending in .e2e-spec.ts and sets up moduleNameMapper so Jest can understand absolute paths starting with src/ when running tests from the test directory located outside the src directory.
Here I will explain the meaning of the moduleNameMapper field as follows:
- Left side - Regex Pattern: ^src/(.*)$
- ^src/: Find all strings starting with src/.
- (.*): Parentheses create a Capture Group. It will "catch" the entire remaining part after src/.
- $: End of string.
- Example: If you import src/service/s3.service, the Capture Group (.*) will be service/s3.service.
- Right side - Target Path: <rootDir>/../src/$1
- <rootDir>: In the test/jest-e2e.json file, rootDir is usually the test directory.
- /../: Jump out one level (from the test directory to the project root directory).
- /src/: Go into the real src directory.
- $1: This is the most important part. It will pour the content that the Capture Group on the left side "caught" into here.
- Example: service/s3.service will be substituted into $1.
Actual workflow:
When Jest sees this line of code in your test file:
It will compare with the config and perform the steps:
- Matches the pattern ^src/(.*)$.
- Takes the service/s3.service part and assigns it to $1.
- Converts the path to: {project}/test/../src/service/s3.service.
- Shortens it back to: {project}/src/service/s3.service.
Update file test-setup.ts
Create file test.controller.spec.ts for unit test
This unit test code checks each method in TestController separately. It uses Mocking techniques to simulate Logger and other dependencies, helping tests run fast and independent of actual external services, while accurately checking return results as well as Exceptions.
Create file test.e2e-spec.ts for end-to-end test
This e2e test code uses the supertest library to send real HTTP requests (GET, POST, PUT, DELETE) to the server running in the test environment. It checks the entire lifecycle of a request, including passing through data processing Pipes and Validation, to ensure the API responds with the correct status code and desired data structure.
Result when running unit test
Result when running end-to-end test
Result when running test coverage, you can see that the config for coverageThreshold has worked when the test coverage has not met the thresholds I requested
When you open the generated html file in the test report, the result will be as follows:
Happy coding!
Comments
Post a Comment