What is @nestjs/testing?
The @nestjs/testing package provides a set of utilities designed to help with the testing of NestJS applications. It simplifies the process of setting up a testing environment, allowing developers to focus on writing their tests. This package is specifically tailored for NestJS, making it easier to test modules, services, controllers, and other components within a NestJS application.
What are @nestjs/testing's main functionalities?
Unit Testing Controllers
This example demonstrates how to set up a unit test for a controller in NestJS using the @nestjs/testing package. It shows the creation of a testing module, mocking dependencies, and asserting the controller's existence.
import { Test, TestingModule } from '@nestjs/testing';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
describe('CatsController', () => {
let controller: CatsController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [CatsController],
providers: [{ provide: CatsService, useValue: {} }],
}).compile();
controller = module.get<CatsController>(CatsController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
Integration Testing
This code snippet illustrates how to perform an integration test on a NestJS application. It involves setting up the entire application within the test environment and using the 'supertest' package to make HTTP requests to test the application's endpoints.
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
describe('Cats', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it('/GET cats', () => {
return request(app.getHttpServer())
.get('/cats')
.expect(200)
.expect('This action returns all cats');
});
});
Other packages similar to @nestjs/testing
jest
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works with projects using: Babel, TypeScript, Node, React, Angular, Vue, and more. Jest is often used in combination with NestJS for unit and integration testing, similar to @nestjs/testing, but it is more general-purpose and not specifically tailored to NestJS.
mocha
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. It is another alternative to @nestjs/testing for writing tests in a NestJS application, but it requires more setup and configuration to integrate smoothly with NestJS.
chai
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework. It's often used alongside Mocha or Jest to provide more expressive assertions. While not a testing framework itself, it complements packages like @nestjs/testing by offering a rich set of assertions that can be used in tests.
A progressive Node.js framework for building efficient and scalable web applications. :cat:
Description
Nest is a framework for building efficient, scalable Node.js web applications. It uses modern JavaScript, is built with TypeScript (preserves compatibility with pure JavaScript) and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reactive Programming).
Under the hood, Nest makes use of Express, allowing for easy use of the myriad third-party plugins which are available.
Philosophy
In recent years, thanks to Node.js, JavaScript has become the “lingua franca” of the web for both front and backend applications, giving rise to awesome projects like Angular, React and Vue which improve developer productivity and enable the construction of fast, testable, extensible frontend applications. However, on the server-side, while there are a lot of superb libraries, helpers and tools for Node, none of them effectively solve the main problem - the architecture.
Nest aims to provide an application architecture out of the box which allows for effortless creation of highly testable, scalable, loosely coupled and easily maintainable applications.
Features
- Built with TypeScript (compatible with pure JavaScript + Babel)
- Easy to learn - syntax similar to Angular
- Familiar - based on well-known libraries (Express / socket.io)
- Dependency Injection - built-in asynchronous IoC container with a hierarchical injector
- WebSockets module (based on socket.io, but you can bring your own library, by making use of
WsAdapter
) - Modular - defines an easy to follow module definition pattern so you can split your system into reusable modules
- Reactive microservice support with message patterns (built-in transport via TCP / Redis, but other communication schemes can be implemented with
CustomTransportStrategy
) - Exception layer - throwable web exceptions with status codes, exception filters
- Pipes - synchronous & asynchronous (e.g. validation purposes)
- Guards - attach additional logic in a declarative manner (e.g. role-based access control)
- Interceptors - built on top of RxJS
- Testing utilities (both e2e & unit tests)
Installation
Install the TypeScript Starter Project with Git:
$ git clone https://github.com/nestjs/typescript-starter.git project
$ cd project
$ npm install
$ npm run start
Install the JavaScript (Babel) Starter Project with Git:
$ git clone https://github.com/nestjs/javascript-starter.git project
$ cd project
$ npm install
$ npm run start
Start a New Project from Scratch with NPM:
$ npm i --save @nestjs/core @nestjs/common @nestjs/microservices @nestjs/websockets @nestjs/testing reflect-metadata rxjs
Documentation & Quick Start
:books: Documentation & Tutorial
Backers
I am on a mission to provide an architecture to create truly flexible, scalable and loosely coupled systems using the Node.js platform. It takes a lot of time, so if you want to support me, please become a backer / sponsor. I appreciate your help. Thanks! :heart_eyes:
People
License
MIT