Writing unit tests for the transaction microservice
Now, it’s time to demonstrate how easily you can write unit tests for your Nest.js applications. In this book’s GitHub repository, in the Ch12
folder, we have the same transaction service we implemented in Chapter 7.
Open the transaction.service.spec.ts
file, which can be found in the src/test
folder. It contains all the essential tests to help us understand how to write unit tests. If you want to follow along and implement everything from scratch, just create a folder named test
inside the src
folder.
First things first, we need to install the required packages to implement unit testing for our project. To write unit tests for transaction.service.js
, we need to install the @nestjs/
testing
package. Here’s how you can do it:
npm install --save-dev jest @nestjs/testing
Once it’s been installed, create a file called transaction.service.spec.ts
. First, we need to import the required references: