home / skills / shipshitdev / library / nestjs-testing-expert

nestjs-testing-expert skill

/bundles/testing/skills/nestjs-testing-expert

This skill helps you build reliable Jest test suites for NestJS modules, services, and controllers with deterministic, fast tests.

npx playbooks add skill shipshitdev/library --skill nestjs-testing-expert

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
978 B
---
name: nestjs-testing-expert
description: Testing patterns for NestJS apps using Jest, covering unit, integration, and e2e tests.
---

# NestJS Testing Expert

You build reliable Jest test suites for NestJS modules, services, and controllers.

## When to Use

- Writing unit or integration tests for NestJS
- Setting up TestModule, mocking providers, or database fakes
- Debugging flaky tests

## Testing Pyramid

- Unit tests for pure logic and services
- Integration tests for modules with real providers
- E2E tests for HTTP APIs

## Common Patterns

- Use `Test.createTestingModule` with explicit providers.
- Mock external services with jest.fn or test doubles.
- For DB: use in-memory adapters or test containers when needed.
- Prefer `supertest` for HTTP-level e2e.

## Tips

- Keep tests deterministic.
- Reset mocks between tests.
- Avoid shared mutable state.

## Checklist

- Clear arrange/act/assert structure
- Minimal mocking
- Covers error paths
- Fast to run

Overview

This skill teaches testing patterns and pragmatic workflows for NestJS applications using Jest, covering unit, integration, and end-to-end (E2E) tests. It focuses on building reliable, fast, and deterministic test suites for modules, services, and controllers. The guidance emphasizes clear structure, minimal mocking, and strategies for dealing with databases and external services.

How this skill works

The skill explains how to construct TestModule instances, register explicit providers, and isolate units under test. It demonstrates mocking external dependencies with jest.fn, using in-memory adapters or test containers for database layers, and running E2E tests through supertest against an application instance. It also outlines test lifecycle practices like resetting mocks and avoiding shared mutable state to prevent flakiness.

When to use it

  • Writing unit tests for services and pure business logic in NestJS
  • Creating integration tests that exercise module wiring and real providers
  • Building E2E tests for HTTP APIs and request/response flows
  • Setting up test doubles for external services and APIs
  • Diagnosing and fixing flaky or slow tests in a NestJS codebase

Best practices

  • Follow arrange-act-assert structure and keep each test focused
  • Mock only what’s necessary; prefer real implementations for integration tests
  • Use in-memory databases or test containers to avoid shared state
  • Reset mocks and clear module state between tests to keep them deterministic
  • Keep test suites fast: split slow E2E tests from fast unit tests

Example use cases

  • Unit testing a service method that performs business calculations with jest.fn mocks for external calls
  • Integration test that boots a TestModule with real repositories and verifies provider injection
  • E2E test using supertest to validate REST endpoints and middleware behavior
  • Replacing a cloud API client with a lightweight test double for CI runs
  • Migrating flaky tests to deterministic patterns by removing shared globals and resetting state

FAQ

Should I use real databases in integration tests?

Prefer in-memory adapters or test containers for integration tests; use real databases sparingly and isolate them to avoid brittle CI runs.

How do I prevent test flakiness?

Reset mocks and module state between tests, avoid shared mutable state, and keep tests deterministic by controlling time and async behavior where needed.