Comprehensive testing utilities for NestJS applications with dependency injection testing capabilities
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core functionality for creating isolated test modules with dependency injection. This is the foundation of NestJS testing, allowing developers to create minimal test environments that mirror production module structures.
The main entry point for creating testing modules. Provides a static method to initialize the test module building process.
/**
* Main entry point for creating testing modules
*/
class Test {
/**
* Creates a TestingModuleBuilder for configuring a test module
* @param metadata - Module metadata defining providers, controllers, imports, etc.
* @param options - Optional configuration for the testing module
* @returns TestingModuleBuilder instance for chaining configuration methods
*/
static createTestingModule(
metadata: ModuleMetadata,
options?: TestingModuleOptions
): TestingModuleBuilder;
}Usage Examples:
import { Test } from "@nestjs/testing";
import { UsersModule } from "./users.module";
import { UsersService } from "./users.service";
// Basic module creation
const moduleBuilder = Test.createTestingModule({
providers: [UsersService],
});
// Module with imports
const moduleWithImports = Test.createTestingModule({
imports: [UsersModule],
providers: [UsersService],
});
// With custom options
const moduleWithOptions = Test.createTestingModule(
{
providers: [UsersService],
},
{
moduleIdGeneratorAlgorithm: "uuid",
}
);Builder pattern class for configuring testing modules. Provides methods for setting up overrides and compiling the final test module.
/**
* Builder for configuring and compiling testing modules
*/
class TestingModuleBuilder {
/**
* Sets a custom logger for the testing module
* @param testingLogger - Logger service implementation
* @returns TestingModuleBuilder for method chaining
*/
setLogger(testingLogger: LoggerService): TestingModuleBuilder;
/**
* Compiles the testing module with all configured overrides
* @param options - Optional compilation options for snapshots and previews
* @returns Promise resolving to a compiled TestingModule
*/
compile(
options?: Pick<NestApplicationContextOptions, "snapshot" | "preview">
): Promise<TestingModule>;
}Usage Examples:
import { Test, TestingModule } from "@nestjs/testing";
import { Logger } from "@nestjs/common";
// Basic compilation
const module: TestingModule = await Test.createTestingModule({
providers: [UsersService],
}).compile();
// With custom logger
const moduleWithLogger = await Test.createTestingModule({
providers: [UsersService],
})
.setLogger(new Logger())
.compile();
// With snapshot for deterministic testing
const moduleWithSnapshot = await Test.createTestingModule({
providers: [UsersService],
}).compile({ snapshot: true });The compiled testing module that provides dependency injection and can create full NestJS applications.
/**
* Compiled testing module extending NestJS application context
*/
class TestingModule extends NestApplicationContext {
/**
* Gets an instance of a provider, controller, or injectable from the module
* @param typeOrToken - Class constructor or injection token
* @param options - Optional resolution options
* @returns Instance of the requested provider
*/
get<TInput = any, TResult = TInput>(
typeOrToken: Type<TInput> | Function | string | symbol,
options?: GetOrResolveOptions
): TResult;
/**
* Resolves an instance asynchronously
* @param typeOrToken - Class constructor or injection token
* @param contextId - Optional context identifier for scoped providers
* @param options - Optional resolution options
* @returns Promise resolving to the requested instance
*/
resolve<TInput = any, TResult = TInput>(
typeOrToken: Type<TInput> | Function | string | symbol,
contextId?: ContextId,
options?: ResolveOptions
): Promise<TResult>;
/**
* Closes the testing module and cleans up resources
* @returns Promise that resolves when cleanup is complete
*/
close(): Promise<void>;
}Usage Examples:
import { Test, TestingModule } from "@nestjs/testing";
import { UsersService } from "./users.service";
import { DatabaseService } from "./database.service";
describe("UsersService", () => {
let module: TestingModule;
let usersService: UsersService;
let databaseService: DatabaseService;
beforeEach(async () => {
module = await Test.createTestingModule({
providers: [UsersService, DatabaseService],
}).compile();
// Get service instances
usersService = module.get<UsersService>(UsersService);
databaseService = module.get<DatabaseService>(DatabaseService);
});
afterEach(async () => {
// Clean up resources
await module.close();
});
it("should retrieve users from database", async () => {
// Test implementation
});
});
// Using resolve for async initialization
const asyncService = await module.resolve<AsyncService>(AsyncService);import { ModuleMetadata } from "@nestjs/common/interfaces/modules/module-metadata.interface";
import { NestApplicationContextOptions } from "@nestjs/common/interfaces/nest-application-context-options.interface";
import { LoggerService, Type } from "@nestjs/common";
import { ContextId } from "@nestjs/core";
interface TestingModuleOptions {
moduleIdGeneratorAlgorithm?: "uuid" | "timestamp";
}
interface GetOrResolveOptions {
strict?: boolean;
each?: boolean;
}
interface ResolveOptions {
strict?: boolean;
}