Comprehensive testing utilities for NestJS applications with dependency injection testing capabilities
npx @tessl/cli install tessl/npm-nestjs--testing@11.1.0NestJS Testing provides comprehensive testing utilities for NestJS applications, enabling developers to create isolated testing environments with sophisticated dependency injection testing capabilities. The package offers tools for building test modules, overriding providers, and creating mock dependencies without bootstrapping the entire application.
npm install @nestjs/testingimport { Test, TestingModule } from "@nestjs/testing";For CommonJS:
const { Test, TestingModule } = require("@nestjs/testing");Additional imports for advanced use cases:
import {
OverrideBy,
OverrideByFactoryOptions,
MockFactory,
TestingModuleBuilder,
TestingModuleOptions,
} from "@nestjs/testing";import { Test, TestingModule } from "@nestjs/testing";
import { UsersService } from "./users.service";
import { UsersController } from "./users.controller";
describe("UsersController", () => {
let controller: UsersController;
let service: UsersService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
providers: [UsersService],
}).compile();
controller = module.get<UsersController>(UsersController);
service = module.get<UsersService>(UsersService);
});
it("should be defined", () => {
expect(controller).toBeDefined();
});
});NestJS Testing is built around several key components:
createTestingModule methodCore functionality for creating isolated test modules with dependency injection. Essential for unit and integration testing of NestJS components.
class Test {
static createTestingModule(
metadata: ModuleMetadata,
options?: TestingModuleOptions
): TestingModuleBuilder;
}
interface TestingModuleOptions {
moduleIdGeneratorAlgorithm?: "uuid" | "timestamp";
}System for replacing providers, guards, pipes, filters, and interceptors with test doubles. Enables precise control over dependencies during testing.
interface OverrideBy {
useValue(value: any): TestingModuleBuilder;
useFactory(options: OverrideByFactoryOptions): TestingModuleBuilder;
useClass(metatype: any): TestingModuleBuilder;
}
interface OverrideByFactoryOptions {
factory: (...args: any[]) => any;
inject?: any[];
}Capability to replace entire modules with alternative implementations for testing scenarios with complex module dependencies.
interface OverrideModule {
useModule(newModule: ModuleDefinition): TestingModuleBuilder;
}Global mocking system with automatic fallback for unresolved dependencies. Integrates with popular mocking libraries for comprehensive test isolation.
type MockFactory = (token?: InjectionToken) => any;
class TestingModuleBuilder {
useMocker(mocker: MockFactory): TestingModuleBuilder;
}Create full NestJS applications and microservices from test modules for end-to-end testing scenarios.
class TestingModule extends NestApplicationContext {
createNestApplication<T extends INestApplication = INestApplication>(
httpAdapter?: HttpServer | AbstractHttpAdapter,
options?: NestApplicationOptions
): T;
createNestMicroservice<T extends object>(
options: NestMicroserviceOptions & T
): INestMicroservice;
}import {
ModuleMetadata,
NestApplicationContextOptions,
InjectionToken,
INestApplication,
INestMicroservice,
HttpServer,
NestApplicationOptions,
LoggerService,
} from "@nestjs/common";
import { AbstractHttpAdapter } from "@nestjs/core";
import { ModuleDefinition } from "@nestjs/core";
import { NestMicroserviceOptions } from "@nestjs/common/interfaces/microservices/nest-microservice-options.interface";
type TestingModuleOptions = Pick<
NestApplicationContextOptions,
"moduleIdGeneratorAlgorithm"
>;
type MockFactory = (token?: InjectionToken) => any;
interface OverrideBy {
useValue(value: any): TestingModuleBuilder;
useFactory(options: OverrideByFactoryOptions): TestingModuleBuilder;
useClass(metatype: any): TestingModuleBuilder;
}
interface OverrideByFactoryOptions {
factory: (...args: any[]) => any;
inject?: any[];
}
interface OverrideModule {
useModule(newModule: ModuleDefinition): TestingModuleBuilder;
}