dariopalminio/agile-sddf-extension/test-nestjs-jest-testing-module
Claude Code and agent skill by dariopalminio/agile-sddf-extension
Trust tier 3
- community
Source check pending
Checking that the SKILL.md file is still in the repository.
Write Unit Tests for NestJS applications using Testing Module and jest
Install
npx vskill@latest install dariopalminio/agile-sddf-extension/test-nestjs-jest-testing-moduleSKILL.md
The same SKILL.md was published earlier as dariopalminio/agile-sddf-extension/test-nestjs-jest-testing-module.
NestJS Jest Testing Module Unit Tests
Overview
Write unit tests for NestJS applications using Test.createTestingModule and Jest.
Capabilities:
- Implement the RED phase tests of TDD.
- Scaffold isolated unit test suites with
Test.createTestingModule(...).compile()and resolve providers viamodule.get(...). - Test services, controllers, guards, interceptors, and pipes in isolation with mocked dependencies (
useValuewithjest.fn()). - Mock external services (HTTP, databases/repositories, SDKs) so tests stay fast, deterministic, and side-effect free.
- Cover edge cases: thrown exceptions (
NotFoundException,ConflictException), API timeouts, rate limiting, and time-dependent logic viajest.useFakeTimers().
Limitations:
- It does not implement production-functional code, it only implements tests.
- Not for API integration tests that exercise the full HTTP request/response cycle (use the
test-nestjs-supertestskill instead). - Not for true browser E2E tests that drive a UI (use the
playwright/cypressskills instead). - Not for frontend component tests (use the
test-react-testing-libraryskill instead). - Does not test against real external services or databases — those must be mocked.
Prerequisites
- A NestJS application with
@nestjs/testingandjestinstalled. - Providers/services under test that receive their dependencies via NestJS dependency injection (constructor injection).
- A Jest config that discovers unit test files (conventionally
*.spec.ts).
Core Workflow
- Locate the spec — co-locate
<name>.spec.tsnext to the class under test, insidesrc/. - Compile a minimal module —
Test.createTestingModule({ providers: [...] }).compile()inbeforeEach, declaring only the class under test plus its direct dependencies. - Mock every dependency —
{ provide: Token, useValue: { method: jest.fn() } }, using the right token (getRepositoryToken,getModelToken, custom@Injecttoken). - Resolve —
module.get<T>(Token)for singletons,await module.resolve<T>(Token)forREQUEST/TRANSIENTscope. - Write the happy path — arrange the stubs, act, assert the returned value and the observable side effects.
- Write the failure paths — thrown exceptions, dependency errors, timeouts, and the absence of side effects on error.
- Clean up —
jest.clearAllMocks()inafterEach;jest.useRealTimers()too when fake timers were used.
Constraints
MUST DO
- Build the module with
Test.createTestingModule(...).compile()— never instantiate the class withnew - Declare only the class under test and its direct dependencies; never import
AppModule - Create the module in
beforeEach, notbeforeAll, so every test starts isolated - Mock every external dependency: HTTP clients, repositories, message queues, SDKs
- Type mocks as
jest.Mocked<T>, or derive the type from a factory withReturnType<typeof factory> - Stub every call the method under test makes, not only the one being asserted
awaitevery promise assertion:await expect(...).rejects.toThrow(...)- Assert the returned value, not merely that a mock was called
- Call
jest.clearAllMocks()inafterEach - Restore timers with
jest.useRealTimers()inafterEachwheneverjest.useFakeTimers()was used - Return observables (
of(...)/throwError(...)) fromHttpServicemocks — not resolved promises - Cover the denial path of every guard, not just the allow path
MUST NOT DO
- Instantiate the class under test manually (
new UsersService(new UserRepository())) - Call real external services or databases from a unit test
- Import
AppModuleinto a unit test module - Leave a promise assertion without
await— the test then passes unconditionally - Ship a mock that only covers the happy path, with no error scenario
- Assert on private methods or properties (
(service as any).buildQuery) - Cast a mock with
as anyto satisfy a wide SDK interface — infer the type from the factory instead - Leave
jest.useFakeTimers()active past the test that needed it
Examples
- Service with a mocked repository — provide
UsersServiceplus auseValuemock forUserRepository, then assert thatservice.create(dto)callsrepo.save(dto)and returns the saved user. See references/test-use-testing-module.md. - Exception paths — mock
repo.findOneto return an existing record and assertservice.create(...)rejects withConflictException; mock it to returnnulland assertfindById(...)rejects withNotFoundException. - Guard testing — compile a module with
RolesGuardplus auseValuemock forReflector, stubreflector.getAllAndOverride, and assertguard.canActivate(context)returns the expected boolean. Always include the denial cases (wrong role, no user at all): a guard that always returnedtruewould pass an allow-only suite. - Mocked external HTTP service — provide a
jest.Mocked<HttpService>, returnof(mockResponse)for success andthrowError(...)for timeout/rate-limit cases. See references/test-mock-external-services.md.
References
For more details, consult these reference files (loaded on demand):
- references/test-mock-external-services.md - Mock external services in tests
- references/test-use-testing-module.md - Use Testing Module for unit tests
These files are only loaded if the agent needs additional context.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/test-nestjs-jest-testing-module/SKILL.md
Popularity
- 7-day trend score
- 5
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/dariopalminio/agile-sddf-extension/test-nestjs-jest-testing-module)