Implement Test-Driven Development (TDD) in Nest.js with Jest
Test-Driven Development is a software development approach where tests are written before the actual implementation code. This iterative process typically involves three phases: Red, Green, and Refactor.
- Red: Write a failing test case that defines the desired behavior or functionality.
- Green: Implement the minimum code required to pass the failing test.
- Refactor: Improve the code without changing its behavior to enhance readability, maintainability, or performance.
This article assumes that you already have a Nest.js project so I won’t explain how to create a Nest.js project.
First, you have to install Jest into the Nest.js project. Run
yarn add @nestjs/testing
Since Nest.js is a Node.js framework developed with a focus on modularity and scalability, so to create a new “service”, you have to create a module. By running
nest generate res <name> [options]
In this command, there are many command flag that are useful for you in the context of creating a module. But overall the command is sufficient to implement TDD this time. You can read the documentation of the flag here https://docs.nestjs.com/cli/usages
After running this command, the system will provide several options. I suggest choosing an option like this:
PS C:\Users\62812\Documents\GitLab\Pegon\pegon-nest> nest generate res finance
? What transport layer do you use? REST API
? Would you like to generate CRUD entry points? Yes
Now, it will generate folder structure like this:
Of course, the name of the module will follow what you input in the previous command. But here the module for example is “finance”
AndAnd because we have selected yes on “Would you like to generate CRUD entry points?”, there is already an endpoint available that we can code later after we create the unit test.
Testing files should have a .spec
or .test
suffix. And will contain this in the first generate
And this is where the TDD begin. As I have said before, TDD is a software development approach where tests are written before the actual implementation code. So you can create test cases that meet the acceptance criteria that have been determined, including happy, failed, and edge cases.
The file also provides an example of how to create a test case. I will also give an example of how to create a test case.
But before that, you can first run the command below to see how jest works in running test cases.
yarn test:cov
kindly reminder to check the package.json to see all the script that can run on your project.
after that, it will generate something like this.
And after that, you can run the command below to be able to run the test automatically if there are changes to the code you have made.
yarn test:watch
and you will also be able to see that jest is “watching” you like this.
You can try by changing several files then saving them, Jest will automatically run the test case automatically.
Start creating test cases
By turning on Jest in “watch” mode, you can swiftly craft test cases and instantly focus on refining your code with rapid feedback loops. This agile approach empowers developers to iteratively enhance their codebase, ensuring robustness and reliability with each incremental change.
So, this is my example unit test for finance service file
And when you hit save, the jest will imidietly run test case and return error or feedback.
And you can see that the test case does not pass the service finance file, especially in the findAll section. Then now you have to develop the findAll function which will produce output that matches the test case.
and after you create the appropriate code and save the file, finally the test case pass
With that, you can create all unit tests and test cases according to existing requirements such as happy cases, failed cases, and edge cases. And, in general, this is how you can quickly, simply, and clearly implement Test-Driven Development (TDD) on Nest.js using Jest.