NestJS

Nest Test.createTestingModule()

Nest testing modules create a controlled dependency graph for unit and integration tests. Provider overrides replace selected dependencies with deterministic fixtures. Use Test.createTestingModule to build a module, override a provider before compilation, and resolve the service. Close the compiled module or app after testing. A provider test does not execute controller routing, pipes or guards. A separate end-to-end test initializes a Nest application with the chosen adapter and exercises the HTTP contract. Apply the same relevant global pipes and guards as the actual app. Static example only; runtime NOT RUN. Source: https://docs.nestjs.com/fundamentals/testing.

Formal Syntax & Structure

nodejs syntaxnodejs
Test.createTestingModule(metadata).overrideProvider(TOKEN).useValue(value).compile()

Working Example

Example Snippetnodejs
import { Injectable, Inject } from '@nestjs/common';import { Test } from '@nestjs/testing';import assert from 'node:assert/strict';const STOCK = Symbol('STOCK');@Injectable()class StockService {  constructor(@Inject(STOCK) private readonly stock: {count():number}) {}  count(){return this.stock.count();}}async function verify(){  const module = await Test.createTestingModule({providers:[StockService,{provide:STOCK,useValue:{count:()=>4}}]})    .overrideProvider(STOCK).useValue({count:()=>0}).compile();  try { assert.equal(module.get(StockService).count(),0); }  finally { await module.close(); }}verify().catch(error=>{console.error(error);process.exitCode=1;});
Node.js Reference