j4flmao/agent_skills_nodejs_nestjs

testing-nestjs-applications

Tests NestJS applications at unit, integration, and e2e levels using Test.createTestingModule and Supertest with hand-rolled fakes.

ソースを見る

使い方

  1. インストールコマンドをコピーし、自分の環境へ導入する前にソースを確認します。
  2. 必要なツール、認証情報、対応環境を原文で確認します。
  3. 入力と成果物を指定し、出力を資料と照合します。
npx skills add https://github.com/j4flmao/agent_skills_nodejs_nestjs --skill testing-nestjs-applications

開始前の確認

  • 導入にはモデル契約、API枠、ファイルへのアクセス許可は含まれません。
  • スター数や導入数は活動の指標であり、独立した品質・安全性試験ではありません。
リポジトリの原文

見出し、例、コード、表、リンク、参照画像を含む原文を表示しています。

Testing NestJS Applications

When to use

  • Writing tests for use cases, controllers, or full HTTP flows
  • Setting up test strategy for a NestJS feature
  • Replacing mocks with hand-rolled fakes

Core rules

  1. Unit tests: domain entities with pure assertions (no NestJS testing module)
  2. Use-cases tested with hand-rolled fakes (not jest.mock)
  3. Integration tests: Test.createTestingModule with real or fake providers
  4. E2E tests: Supertest against bootstrapped app
  5. No jest.mock for internal modules; only for third-party if needed

Reference shape (TypeScript)

Unit Test (Domain Entity)

typescript
import { User } from './user.entity';

describe('User', () => {
  it('creates with valid email', () => {
    const email = Email.create('test@example.com');
    const user = User.create('1', email);
    expect(user.id).toBe('1');
  });
});

Integration Test (Use-Case with Fake)

typescript
import { Test } from '@nestjs/testing';
import { CreateUserUseCase } from './create-user.use-case';
import { FakeUserRepository } from './fakes/fake-user.repository';

describe('CreateUserUseCase', () => {
  let useCase: CreateUserUseCase;

  beforeEach(async () => {
    const module = await Test.createTestingModule({
      providers: [
        CreateUserUseCase,
        { provide: 'UserRepository', useClass: FakeUserRepository },
      ],
    }).compile();
    useCase = module.get(CreateUserUseCase);
  });

  it('creates user', async () => {
    const result = await useCase.execute({ id: '1', email: 'test@example.com' });
    expect(result.success).toBe(true);
  });
});

Examples — Do

typescript
const module = await Test.createTestingModule({
  controllers: [UserController],
  providers: [CreateUserUseCase, { provide: 'UserRepository', useClass: FakeUserRepository }],
}).compile();

Examples — Don't

typescript
// ❌ jest.mock for internal modules
jest.mock('../../application/user.repository');

Checklist

  • [ ] Domain entities have unit tests
  • [ ] Use-cases tested with hand-rolled fakes
  • [ ] Integration tests with Test.createTestingModule
  • [ ] E2E tests with Supertest

See reference/test-patterns.md for full patterns.

タスクに関連する項目

同じリポジトリから

関連する Skills

すべての Skills