AgentSkillsCN

typescript-unit-testing

针对 TypeScript/NestJS 项目,运用 Jest、@golevelup/ts-jest 以及内存数据库,打造完整的单元测试技能。 当用户有以下需求时,请务必使用此技能: **设置**——初始化或配置单元测试: - 为新项目搭建 Jest 测试环境 - 配置测试基础设施(jest.config.ts) - 安装测试依赖(@nestjs/testing、@golevelup/ts-jest) - 创建模拟辅助工具或测试实用程序 - 设置代码覆盖率配置 **编写**——创建或新增单元测试: - 编写、创建、添加或生成单元测试 - 测试服务、用例、控制器、守卫、拦截器、管道或过滤器 - 为新代码或新功能添加测试 - 提升测试覆盖率或补全缺失的测试 - 模拟依赖项或设置测试用例 - 处理任何以 .spec.ts 结尾的文件 **审查**——审计或评估单元测试: - 审查现有测试的质量 - 检查测试覆盖率与测试盲区 - 审核测试模式与规范 - 评估断言的质量 **运行**——执行或分析测试结果: - 运行单元测试 - 分析测试结果或覆盖率报告 - 理解测试的通过与失败情况 - 查看哪些测试成功,哪些测试失败 **调试**——修复失败或损坏的测试: - 修复失败的单元测试 - 调试测试中的错误或异常 - 解决模拟问题或配置问题 - 排查测试超时或易出错的测试 - 诊断“未定义”或意外的结果 **优化**——提升测试性能与可维护性: - 加速运行缓慢的测试 - 修复未正确关闭的句柄,避免影响干净退出 - 改善测试组织结构 - 缩短测试执行时间 - 清理测试代码 关键词:单元测试、spec、Jest、TypeScript、NestJS、Mock、DeepMocked、createMock、AAA、测试覆盖率、TDD、.spec.ts、测试、编写测试、添加测试、创建测试、修复测试、调试测试、运行测试、审查测试、优化测试、测试设置、Jest 配置

SKILL.md
--- frontmatter
name: typescript-unit-testing
description: |
  Complete unit testing skill for TypeScript/NestJS projects using Jest, @golevelup/ts-jest, and in-memory databases.

  ALWAYS use this skill when user needs to:

  **SETUP** - Initialize or configure unit testing:
  - Set up Jest for a new project
  - Configure test infrastructure (jest.config.ts)
  - Install testing dependencies (@nestjs/testing, @golevelup/ts-jest)
  - Create mock helpers or test utilities
  - Set up coverage configuration

  **WRITE** - Create or add unit tests:
  - Write, create, add, or generate unit tests
  - Test a service, usecase, controller, guard, interceptor, pipe, or filter
  - Add tests for new code or features
  - Improve test coverage or add missing tests
  - Mock dependencies or set up test fixtures
  - Working on any file ending in .spec.ts

  **REVIEW** - Audit or evaluate unit tests:
  - Review existing tests for quality
  - Check test coverage and gaps
  - Audit testing patterns and conventions
  - Evaluate assertion quality

  **RUN** - Execute or analyze test results:
  - Run unit tests
  - Analyze test results or coverage reports
  - Understand test failures or successes
  - Check which tests are passing/failing

  **DEBUG** - Fix failing or broken tests:
  - Fix failing unit tests
  - Debug test errors or exceptions
  - Resolve mock issues or setup problems
  - Troubleshoot test timeouts or flaky tests
  - Diagnose "undefined" or unexpected results

  **OPTIMIZE** - Improve test performance and maintainability:
  - Speed up slow tests
  - Fix open handles preventing clean exit
  - Improve test organization
  - Reduce test execution time
  - Clean up test code

  Keywords: unit test, spec, jest, typescript, nestjs, mock, DeepMocked, createMock, AAA, test coverage, TDD, .spec.ts, testing, write test, add test, create test, fix test, debug test, run test, review test, optimize test, test setup, jest config

Unit Testing Skill

Unit testing validates individual functions, methods, and classes in isolation by mocking all external dependencies.


Workflows

For guided, step-by-step execution of unit testing tasks, use the appropriate workflow:

WorkflowPurposeWhen to Use
SetupInitialize test infrastructureNew project or missing test setup
WritingWrite new unit testsCreating tests for components
ReviewingReview existing testsCode review, quality audit
RunningExecute testsRunning tests, analyzing results
DebuggingFix failing testsTests failing, need diagnosis
OptimizingImprove test performanceSlow tests, maintainability

Workflow Selection Guide

IMPORTANT: Before starting any testing task, identify the user's intent and load the appropriate workflow.

Detect User Intent → Select Workflow

User Says / WantsWorkflow to LoadFile
"Set up tests", "configure Jest", "add testing to project", "install test dependencies"Setupworkflows/setup/workflow.md
"Write tests", "add tests", "create tests", "test this service/controller"Writingworkflows/writing/workflow.md
"Review tests", "check test quality", "audit tests", "are these tests good?"Reviewingworkflows/reviewing/workflow.md
"Run tests", "execute tests", "check if tests pass", "show test results"Runningworkflows/running/workflow.md
"Fix tests", "debug tests", "tests are failing", "why is this test broken?"Debuggingworkflows/debugging/workflow.md
"Speed up tests", "optimize tests", "tests are slow", "fix open handles"Optimizingworkflows/optimizing/workflow.md

Workflow Execution Protocol

  1. ALWAYS load the workflow file first - Read the full workflow before taking action
  2. Follow each step in order - Complete checkpoints before proceeding
  3. Load knowledge files as directed - Each workflow specifies which references/ files to read
  4. Verify compliance after completion - Re-read relevant reference files to ensure quality

Knowledge Base Structure

code
references/
├── common/              # Core testing fundamentals
│   ├── knowledge.md     # Testing philosophy and test pyramid
│   ├── rules.md         # Mandatory testing rules (AAA, naming, coverage)
│   ├── assertions.md    # Assertion patterns and matchers
│   ├── examples.md      # Comprehensive examples by category
│   ├── detect-open-handles.md   # Open handle detection and cleanup
│   └── performance-optimization.md  # Jest runtime optimization
│
├── nestjs/              # NestJS component testing
│   ├── services.md      # Service/usecase testing patterns
│   ├── controllers.md   # Controller testing patterns
│   ├── guards.md        # Guard testing patterns
│   ├── interceptors.md  # Interceptor testing patterns
│   └── pipes-filters.md # Pipe and filter testing
│
├── mocking/             # Mock patterns and strategies
│   ├── deep-mocked.md   # @golevelup/ts-jest patterns
│   ├── jest-native.md   # Jest.fn, spyOn, mock patterns
│   └── factories.md     # Test data factory patterns
│
├── repository/          # Repository testing
│   ├── mongodb.md       # mongodb-memory-server patterns
│   └── postgres.md      # pg-mem patterns
│
├── kafka/               # NestJS Kafka microservices testing
│   └── kafka.md         # ClientKafka, @MessagePattern, @EventPattern handlers
│
└── redis/               # Redis cache testing
    └── redis.md         # Cache operations, health checks, graceful degradation

Quick Reference by Task

Write Unit Tests

  1. MANDATORY: Read references/common/rules.md - AAA pattern, naming, coverage
  2. Read references/common/assertions.md - Assertion best practices
  3. Read component-specific files:
    • Services: references/nestjs/services.md
    • Controllers: references/nestjs/controllers.md
    • Guards: references/nestjs/guards.md
    • Interceptors: references/nestjs/interceptors.md
    • Pipes/Filters: references/nestjs/pipes-filters.md

Setup Mocking

  1. Read references/mocking/deep-mocked.md - DeepMocked patterns
  2. Read references/mocking/jest-native.md - Native Jest patterns
  3. Read references/mocking/factories.md - Test data factories

Test Repositories

  1. MongoDB: references/repository/mongodb.md
  2. PostgreSQL: references/repository/postgres.md

Test Kafka (NestJS Microservices)

  • Read references/kafka/kafka.md - ClientKafka mocking, @MessagePattern/@EventPattern handlers, emit/send testing

Test Redis

  • Read references/redis/redis.md - Cache operations, health checks, graceful degradation

Examples

  • Read references/common/examples.md for comprehensive patterns

Optimize Test Performance

  1. Read references/common/performance-optimization.md - Worker config, caching, CI optimization
  2. Read references/common/detect-open-handles.md - Fix open handles preventing clean exit

Debug Open Handles

  • Read references/common/detect-open-handles.md - Detection commands, common handle types, cleanup patterns

Core Principles

0. Context Efficiency (Temp File Output)

ALWAYS redirect unit test output to temp files, NOT console. Test output can be verbose and bloats agent context.

IMPORTANT: Use unique session ID in filenames to prevent conflicts when multiple agents run.

bash
# Initialize session (once at start of testing session)
export UT_SESSION=$(date +%s)-$$

# Standard pattern - redirect output to temp file (NO console output)
npm test > /tmp/ut-${UT_SESSION}-output.log 2>&1

# Read summary only (last 50 lines)
tail -50 /tmp/ut-${UT_SESSION}-output.log

# Get failure details
grep -B 2 -A 15 "FAIL\|✕" /tmp/ut-${UT_SESSION}-output.log

# Cleanup when done
rm -f /tmp/ut-${UT_SESSION}-*.log /tmp/ut-${UT_SESSION}-*.md

Temp Files (with ${UT_SESSION} unique per agent):

  • /tmp/ut-${UT_SESSION}-output.log - Full test output
  • /tmp/ut-${UT_SESSION}-failures.md - Tracking file for one-by-one fixing
  • /tmp/ut-${UT_SESSION}-debug.log - Debug runs
  • /tmp/ut-${UT_SESSION}-verify.log - Verification runs
  • /tmp/ut-${UT_SESSION}-coverage.log - Coverage output

1. AAA Pattern (Mandatory)

ALL unit tests MUST follow Arrange-Act-Assert:

typescript
it('should return user when found', async () => {
  // Arrange
  const userId = 'user-123';
  mockRepository.findById.mockResolvedValue({
    id: userId,
    email: 'test@example.com',
    name: 'Test User',
  });

  // Act
  const result = await target.getUser(userId);

  // Assert
  expect(result).toEqual({
    id: userId,
    email: 'test@example.com',
    name: 'Test User',
  });
  expect(mockRepository.findById).toHaveBeenCalledWith(userId);
});

2. Use target for SUT

Always name the system under test as target:

typescript
let target: UserService;
let mockRepository: DeepMocked<UserRepository>;

3. DeepMocked Pattern

Use @golevelup/ts-jest for type-safe mocks:

typescript
import { createMock, DeepMocked } from '@golevelup/ts-jest';

let mockService: DeepMocked<UserService>;

beforeEach(() => {
  mockService = createMock<UserService>();
});

4. Specific Assertions

Assert exact values, not just existence:

typescript
// WRONG
expect(result).toBeDefined();
expect(result.id).toBeDefined();

// CORRECT
expect(result).toEqual({
  id: 'user-123',
  email: 'test@example.com',
  name: 'Test User',
});

5. Mock All Dependencies

Mock external services, never real databases for unit tests:

typescript
// Unit Test: Mock repository
{ provide: UserRepository, useValue: mockRepository }

// Repository Test: Use in-memory database
const mongoServer = await createMongoMemoryServer();

Standard Test Template

typescript
import { Test, TestingModule } from '@nestjs/testing';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { MockLoggerService } from 'src/shared/logger/services/mock-logger.service';

describe('UserService', () => {
  let target: UserService;
  let mockRepository: DeepMocked<UserRepository>;

  beforeEach(async () => {
    // Arrange: Create mocks
    mockRepository = createMock<UserRepository>();

    const module: TestingModule = await Test.createTestingModule({
      providers: [
        UserService,
        { provide: UserRepository, useValue: mockRepository },
      ],
    })
      .setLogger(new MockLoggerService())
      .compile();

    target = module.get<UserService>(UserService);
  });

  afterEach(() => {
    jest.clearAllMocks();
  });

  describe('getUser', () => {
    it('should return user when found', async () => {
      // Arrange
      mockRepository.findById.mockResolvedValue({
        id: 'user-123',
        email: 'test@example.com',
      });

      // Act
      const result = await target.getUser('user-123');

      // Assert
      expect(result).toEqual({ id: 'user-123', email: 'test@example.com' });
    });

    it('should throw NotFoundException when user not found', async () => {
      // Arrange
      mockRepository.findById.mockResolvedValue(null);

      // Act & Assert
      await expect(target.getUser('invalid')).rejects.toThrow(NotFoundException);
    });
  });
});

Test Coverage Requirements

CategoryPriorityDescription
Happy pathMANDATORYValid inputs producing expected outputs
Edge casesMANDATORYEmpty arrays, null values, boundaries
Error casesMANDATORYNot found, validation failures
Exception behaviorMANDATORYCorrect type, error code, message
Business rulesMANDATORYDomain logic, calculations
Input validationMANDATORYInvalid inputs, type mismatches

Coverage Target: 80%+ for new code


Failure Resolution Protocol

CRITICAL: Fix ONE test at a time. NEVER run full suite repeatedly while fixing.

When unit tests fail:

  1. Initialize session (once at start):
    bash
    export UT_SESSION=$(date +%s)-$$
    
  2. Create tracking file: /tmp/ut-${UT_SESSION}-failures.md with all failing tests
  3. Select ONE failing test - work on only this test
  4. Run ONLY that test (never full suite):
    bash
    npm test -- -t "test name" > /tmp/ut-${UT_SESSION}-debug.log 2>&1
    tail -50 /tmp/ut-${UT_SESSION}-debug.log
    
  5. Fix the issue - analyze error, make targeted fix
  6. Verify fix - run same test 3-5 times:
    bash
    for i in {1..5}; do npm test -- -t "test name" > /tmp/ut-${UT_SESSION}-run$i.log 2>&1 && echo "Run $i: PASS" || echo "Run $i: FAIL"; done
    
  7. Mark as FIXED in tracking file
  8. Move to next failing test - repeat steps 3-7
  9. Run full suite ONLY ONCE after ALL individual tests pass
  10. Cleanup: rm -f /tmp/ut-${UT_SESSION}-*.log /tmp/ut-${UT_SESSION}-*.md

WHY: Running full suite wastes time and context. Each failing test pollutes output, making debugging harder.


Naming Conventions

Test Files

  • Pattern: *.spec.ts
  • Location: Co-located with source file

Test Structure

typescript
describe('ClassName', () => {
  describe('methodName', () => {
    it('should [expected behavior] when [condition]', () => {});
  });
});

Variable Names

VariableConvention
SUTtarget
Mocksmock prefix (mockRepository, mockService)
Mock TypeDeepMocked<T>

What NOT to Unit Test

Do NOT create unit tests for:

  • Interfaces - Type definitions only, no runtime behavior
  • Enums - Static value mappings, no logic to test
  • Constants - Static values, no behavior
  • Type aliases - Type definitions only
  • Plain DTOs - Data structures without logic

Only test files containing executable logic (classes with methods, functions with behavior).


Anti-Patterns to Avoid

Don'tWhyDo Instead
Assert only existenceDoesn't catch wrong valuesAssert specific values
Conditional assertionsNon-deterministicSeparate test cases
Test private methodsCouples to implementationTest via public interface
Share state between testsCauses flaky testsFresh setup in beforeEach
Mock repositories in servicesTests implementationMock interfaces
Skip mock verificationDoesn't validate behaviorVerify mock calls
Test interfaces/enums/constantsNo behavior to testSkip these files

Checklist

Setup:

  • Use target for system under test
  • Use mock prefix for all mocks
  • Use DeepMocked<T> type
  • Include .setLogger(new MockLoggerService())
  • Follow AAA pattern with comments
  • Reset mocks in afterEach

Coverage:

  • Happy path tests for all public methods
  • Edge case tests (empty, null, boundaries)
  • Error case tests (not found, validation failures)
  • Exception type and error code verification
  • Mock call verification (parameters + count)

Quality:

  • 80%+ coverage on new code
  • No assertions on log calls
  • No test interdependence
  • Tests fail when any field differs