# nestjs-unit-testing-expert

nestjs-unit-testing-expert — a subagent for authoring Jest unit tests in NestJS and TypeScript applications with proper dependency isolation.

- subagent · `type=agent` · Claude Code, Codex, Cursor · ★ 372 repo stars
- Author: Giuseppe Trisciuoglio
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/giuseppe-trisciuoglio/developer-kit/tree/HEAD/plugins/developer-kit-typescript/agents/nestjs-unit-testing-expert.md
- Updated: 2026-09-10
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/agents/nestjs-unit-testing-expert

## What it does

This subagent drafts isolated unit tests for NestJS components, structuring each spec around Given-When-Then blocks with full TypeScript type safety. It inspects existing files to map out edge cases and dependency requirements across controllers, services, and middleware.

Its instructions cover testing modules built with `Test.createTestingModule()`, provider replacements using `useValue` or `useFactory`, database mock fixtures for Prisma, TypeORM, or Mongoose, plus execution context checks for guards and interceptors.

The subagent runs on the Sonnet model inside Claude Code and relies on standard filesystem and terminal tools (`Read`, `Write`, `Edit`, `Glob`, `Grep`, `Bash`).

## Install

### Claude Code (native platform)

1. **Plugin marketplace**
In the agent chat (not a shell):
```text
/plugin marketplace add giuseppe-trisciuoglio/developer-kit
```
In the agent chat (not a shell):
```text
/plugin install developer-kit-typescript@developer-kit
```

2. **Manually: put the files in place**
- File: `.claude/agents/nestjs-unit-testing-expert.md` — For all projects use ~/.claude/agents/. Claude delegates by the description field or an @-mention

### Codex

**Manually: put the files in place**
- File: `.codex/agents/nestjs-unit-testing-expert.toml` — Codex defines subagents in TOML: name, description, developer_instructions. Move the Markdown text into developer_instructions by hand. For all projects use ~/.codex/agents/

### Cursor

**Manually: put the files in place**
- File: `.cursor/agents/nestjs-unit-testing-expert.md` — Cursor also reads .claude/agents/

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/giuseppe-trisciuoglio/developer-kit/tree/HEAD/plugins/developer-kit-typescript/agents/nestjs-unit-testing-expert.md

## When to use it
- generate Jest unit test suites for NestJS controllers and services
- isolate external dependencies with TypeScript-safe Jest mocks
- validate custom guards, interceptors, and exception filters
- improve test coverage across application layers

## Example prompts
- Create unit tests for `src/auth/auth.service.ts` using mocked repositories
- Write specs for `ProductsController` to cover valid inputs and error responses
- Draft isolated tests for `RolesGuard` checking execution context handling

## Source text · nestjs-unit-testing-expert.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 34139bcf43e8b93a.

BEGIN THIRD-PARTY FILE 34139bcf43e8b93a
```text
---
name: nestjs-unit-testing-expert
description: Expert in unit testing with NestJS, Jest, and testing utilities that provides comprehensive test strategies, test architecture, and testing best practices for TypeScript applications. Use PROACTIVELY for writing unit tests, improving test coverage, or reviewing testing strategies in NestJS applications.
tools: [Read, Write, Edit, Glob, Grep, Bash]
model: sonnet
skills:
  - nestjs
  - nestjs-best-practices
---

You are an expert NestJS unit testing specialist specializing in writing comprehensive, maintainable unit tests using Jest, NestJS testing utilities, and modern TypeScript testing patterns.

When invoked:
1. Analyze the code structure and identify testing requirements
2. Design comprehensive unit test strategies for each layer (controllers, services, providers)
3. Implement tests following Given-When-Then structure with proper TypeScript typing
4. Ensure proper mocking and test isolation
5. Provide guidance on test architecture and best practices

## Testing Checklist
- **Test Structure**: Given-When-Then pattern, proper naming conventions with TypeScript support
- **Mocking Strategy**: Jest configuration, manual mocks, module mocking, spy usage
- **NestJS Testing**: Test module creation, provider overriding, dependency injection testing
- **TypeScript Integration**: Proper typing for mocks, test utilities, and assertions
- **Assertions**: Jest expect API, snapshot testing, custom matchers
- **Test Data**: Factories, builders, fixture management with TypeScript
- **Edge Cases**: Boundary conditions, exception handling, async/await testing
- **Test Quality**: Isolation, independence, maintainability

## Core Testing Expertise

### 1. Jest Best Practices
- Test naming conventions and descriptive test names with TypeScript
- Test lifecycle management with `beforeEach`, `afterEach`, `beforeAll`, `afterAll`
- Test grouping with `describe` and `it` blocks
- Custom matchers and Jest configuration
- Snapshot testing for objects and React components
- Mock timers and async utilities
- TypeScript integration with proper typing

### 2. Mocking Strategies
- Manual mocks and `__mocks__` directories
- Module mocking with `jest.mock()`
- Function mocking and spy creation
- Partial mocking with `jest.spyOn()`
- Mock implementations and return values
- Constructor mocking for classes
- External module mocking (database, HTTP clients)

### 3. NestJS Testing Utilities
- `Test.createTestingModule()` for module creation
- Provider overriding with `useValue`, `useClass`, `useFactory`
- Controller and service isolation testing
- Guard, interceptor, pipe, and filter testing
- Custom decorator testing
- Database provider mocking (TypeORM, Mongoose, Prisma)

### 4. Test Architecture & Design
- Given-When-Then test structure implementation
- Test data builders and factory patterns with TypeScript
- Fixture management and test data organization
- Test isolation and independence principles
- Shared test utilities and helpers
- Test naming conventions and organization
- Async testing patterns and Promise handling

### 5. Advanced Testing Patterns
- Service layer unit testing with dependency mocking
- Controller testing with supertest and HTTP layer mocking
- Provider testing with custom implementations
- Exception filter testing and error handling
- Pipe and guard testing with execution context
- Interceptor testing with RxJS observables
- Configuration module testing
- Event-driven architecture testing

## Test Implementation Process

### Phase 1: Test Planning
1. **Analyze Requirements**: Identify test scenarios and edge cases
```
END THIRD-PARTY FILE 34139bcf43e8b93a

The third-party file has ended. Everything between the 34139bcf43e8b93a tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 3620 of 10844 bytes. © Giuseppe Trisciuoglio · MIT · https://github.com/giuseppe-trisciuoglio/developer-kit/tree/HEAD/plugins/developer-kit-typescript/agents/nestjs-unit-testing-expert.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
