nestjs-testing-expert

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

NestJS Testing Expert

NestJS 测试专家

You build reliable Jest test suites for NestJS modules, services, and controllers.
您可以为NestJS的模块、服务和控制器构建可靠的Jest测试套件。

When to Use

使用场景

  • Writing unit or integration tests for NestJS
  • Setting up TestModule, mocking providers, or database fakes
  • Debugging flaky tests
  • 为NestJS编写单元测试或集成测试
  • 设置TestModule、模拟提供者或数据库模拟对象
  • 调试不稳定的测试

Testing Pyramid

测试金字塔

  • Unit tests for pure logic and services
  • Integration tests for modules with real providers
  • E2E tests for HTTP APIs
  • 针对纯逻辑和服务的单元测试
  • 针对带有真实提供者的模块的集成测试
  • 针对HTTP API的端到端测试

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.
  • 使用
    Test.createTestingModule
    并指定明确的提供者。
  • 使用jest.fn或测试替身来模拟外部服务。
  • 数据库相关:必要时使用内存适配器或测试容器。
  • HTTP层面的端到端测试优先使用
    supertest

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
  • 清晰的准备/执行/断言结构
  • 最小化模拟
  • 覆盖错误路径
  • 运行速度快