NestJs is a powerful Node.js framework that allows developers to build scalable and maintainable server-side applications. However, writing unit tests is an essential aspect of software development that ensures the quality of code and helps to detect and fix bugs early in the development process. In this article, we will dive into the world of unit testing with NestJs and learn some best practices to write effective and efficient tests to ensure the quality of our applications.
Before getting started, if you want to set up a basic NestJs application, check out the Getting Started With NestJs: A Beginner’s Guide article here.
Table of Contents
NestJs Unit Testing Best Practices
Let’s start diving into setting up the testing environment and writing NestJs unit testings with some best practices.
Setting up the Test Environment
The first step in writing unit tests is to set up the test environment. NestJs provides a powerful testing module called @nestjs/testing
that allows us to create a testing module and instantiate our controllers, services, and other providers.
Let’s create a new file called app.service.spec.ts
in the src
directory and add the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // app.service.spec.ts import { Test, TestingModule } from '@nestjs/testing'; import { AppService } from './app.service'; describe('AppService', () => { let appService: AppService; beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ providers: [AppService], }).compile(); appService = app.get<AppService>(AppService); }); describe('getHello', () => { it('should return "Hello, World!"', () => { expect(appService.getHello()).toBe('Hello, World!'); }); }); }); |
In this code, we have created a new testing module using the Test.createTestingModule
method and passed our AppService
as a provider. In the beforeEach
hook, we have instantiated our AppService
using the app.get
method.
Next, we have written a test case to test the getHello
method of the AppService
. We expect the getHello
method to return the string "Hello, World!"
.
Testing Controllers
Let’s now write a unit test for a controller. Create a new file called app.controller.spec.ts
in the src
directory and add the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // app.controller.spec.ts import { Test, TestingModule } from '@nestjs/testing'; import { AppController } from './app.controller'; import { AppService } from './app.service'; describe('AppController', () => { let appController: AppController; beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ controllers: [AppController], providers: [AppService], }).compile(); appController = app.get<AppController>(AppController); }); describe('getHello', () => { it('should return "Hello, World!"', () => { expect(appController.getHello()).toBe('Hello, World!'); }); }); }); |
In this code, we have created a new testing module and passed our AppController
and AppService
as providers. We have then instantiated our AppController
using the app.get
method.
Next, we have written a test case to test the getHello
method of the AppController
. We expect the getHello
method to return the string "Hello, World!"
.
Testing Services with Dependencies
Sometimes, our services may have dependencies on other services or modules. In such cases, we can use the overrideProvider
method of the testing module to override the provider with a mock implementation.
Let’s create a new file called app.service.spec.ts
in the src
directory and add the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // app.service.spec.ts import { Test, TestingModule } from '@nestjs/testing'; import { AppService } from './app.service'; describe('AppService', () => { let appService: AppService; beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ providers: [ AppService, { provide: 'ConfigService', useValue: { get: () => 'test', }, }, ], }).compile(); appService = app.get<AppService>(AppService); }); describe('getHello', () => { it('should return "Hello, World!"', () => { expect(appService.getHello()).toBe('Hello, World!'); }); }); }); |
In this code, we have created a new testing module and passed our AppService
and a mock implementation of the ConfigService
as providers. We have then instantiated our AppService
using the app.get
method.
Running the Tests
To run the tests, execute the following command in your terminal:
1 2 3 | npm run test |

Conclusion
In this article, we have learned how to write effective and efficient unit tests for NestJs applications. We have covered testing controllers, services, and services with dependencies. Writing unit tests is an essential aspect of software development that ensures the quality of our code and helps to detect and fix bugs early in the development process. With the powerful testing module provided by NestJs, we can easily write and run tests for our applications.