Przejdź do treści
Biblioteka skilli
Programowanie · Testowanie

typescript-unit-testing

/typescript-unit-testing

Prowadzi testy jednostkowe projektów TypeScript i NestJS z Jest, @golevelup/ts-jest i bazami w pamięci według struktury AAA, w tym mockowanie i naprawę niestabilnych testów.

Repozytorium
skills ↗
Licencja
MIT
Ostatnia zmiana repozytorium
22.10.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
81
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
0
Przestarzałość

Brak sygnałów. Repozytorium zmienione 22.10.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co robi i kiedy

Prowadzi testy jednostkowe projektów TypeScript i NestJS z Jest, @golevelup/ts-jest i bazami w pamięci według struktury AAA, w tym mockowanie i naprawę niestabilnych testów.

Oryginalny opis (EN)

Unit testing for TypeScript/NestJS projects using Jest, @golevelup/ts-jest (DeepMocked/createMock), and in-memory databases, with AAA structure. Use whenever the user is working on `.spec.ts` files or asks to set up Jest, write/add tests for a service/usecase/controller/guard/interceptor/pipe/filter, mock dependencies, review test quality or coverage, run unit tests, debug failing or flaky tests, or optimize test performance and open handles.

Jak działa

  1. Workflows
  2. Workflow Selection Guide
  3. Knowledge Base Structure
  4. Quick Reference by Task
  5. Core Principles
  6. Standard Test Template
  7. Test Coverage Requirements
  8. Failure Resolution Protocol
  9. Naming Conventions
  10. What NOT to Unit Test
Sekcje z nagłówków SKILL.md, w oryginale · 399 linii, 1 729 słów
Podgląd SKILL.md
---
name: typescript-unit-testing
description: Unit testing for TypeScript/NestJS projects using Jest, @golevelup/ts-jest (DeepMocked/createMock), and in-memory databases, with AAA structure. Use whenever the user is working on `.spec.ts` files or asks to set up Jest, write/add tests for a service/usecase/controller/guard/interceptor/pipe/filter, mock dependencies, review test quality or coverage, run unit tests, debug failing or flaky tests, or optimize test performance and open handles.
---

# 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:

| Workflow | Purpose | When to Use |
|----------|---------|-------------|
| [Setup](workflows/setup/workflow.md) | Initialize test infrastructure | New project or missing test setup |
| [Writing](workflows/writing/workflow.md) | Write new unit tests | Creating tests for components |
| [Reviewing](workflows/reviewing/workflow.md) | Review existing tests | Code review, quality audit |
| [Running](workflows/running/workflow.md) | Execute tests | Running tests, analyzing results |
| [Debugging](workflows/debugging/workflow.md) | Fix failing tests | Tests failing, need diagnosis |
| [Optimizing](workflows/optimizing/workflow.md) | Improve test performance | Slow 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 / Wants | Workflow to Load | File |
|-------------------|------------------|------|
| "Set up tests", "configure Jest", "add testing to project", "install test dependencies" | **Setup** | `workflows/setup/workflow.md` |
| "Write tests", "add tests", "create tests", "test this service/controller" | **Writing** | `workflows/writing/workflow.md` |
| "Review tests", "check test quality", "audit tests", "are these tests good?" | **Reviewing** | `workflows/reviewing/workflow.md` |
| "Run tests", "execute tests", "check if tests pass", "show test results" | **Running** | `workflows/running/workflow.md` |
| "Fix tests", "debug tests", "tests are failing", "why is this test broken?" | **Debugging** | `workflows/debugging/workflow.md` |
| "Speed up tests", "optimize tests", "tests are slow", "fix open handles" | **Optimizing** | `workflows/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

```
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

| Category | Priority | Description |
|----------|----------|-------------|
| Happy path | MANDATORY | Valid inputs producing expected outputs |
| Edge cases | MANDATORY | Empty arrays, null values, boundaries |
| Error cases | MANDATORY | Not found, validation failures |
| Exception behavior | MANDATORY | Correct type, error code, message |
| Business rules | MANDATORY | Domain logic, calculations |
| Input validation | MANDATORY | Invalid 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
| Variable | Convention |
|----------|------------|
| SUT | `target` |
| Mocks | `mock` prefix (`mockRepository`, `mockService`) |
| Mock Type | `DeepMocked<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't | Why | Do Instead |
|-------|-----|------------|
| Assert only existence | Doesn't catch wrong values | Assert specific values |
| Conditional assertions | Non-deterministic | Separate test cases |
| Test private methods | Couples to implementation | Test via public interface |
| Share state between tests | Causes flaky tests | Fresh setup in beforeEach |
| Mock repositories in services | Tests implementation | Mock interfaces |
| Skip mock verification | Doesn't validate behavior | Verify mock calls |
| Test interfaces/enums/constants | No behavior to test | Skip 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
Pliki w paczce (81)
  • LICENSE 1 KB
  • SKILL.md 14 KB
  • references/common/assertions.md 7 KB
  • references/common/detect-open-handles.md 9 KB
  • references/common/examples.md 14 KB
  • references/common/knowledge.md 3 KB
  • references/common/performance-optimization.md 8 KB
  • references/common/rules.md 7 KB
  • references/kafka/kafka.md 17 KB
  • references/mocking/deep-mocked.md 5 KB
  • references/mocking/factories.md 7 KB
  • references/mocking/jest-native.md 7 KB
  • references/nestjs/controllers.md 7 KB
  • references/nestjs/guards.md 9 KB
  • references/nestjs/interceptors.md 10 KB
  • references/nestjs/pipes-filters.md 12 KB
  • references/nestjs/services.md 9 KB
  • references/redis/redis.md 13 KB
  • references/repository/mongodb.md 9 KB
  • references/repository/postgres.md 12 KB
  • workflows/debugging/workflow.md 3 KB
  • workflows/debugging/steps/step-01-init.md 3 KB
  • workflows/debugging/steps/step-01b-continue.md 2 KB
  • workflows/debugging/steps/step-02-classify-failure.md 2 KB
  • workflows/debugging/steps/step-03-analyze.md 4 KB
  • workflows/debugging/steps/step-04-implement-fix.md 2 KB
  • workflows/debugging/steps/step-05-verify-fix.md 2 KB
  • workflows/debugging/steps/step-06-document-resolution.md 4 KB
  • workflows/debugging/templates/output-template.md 1 KB
  • workflows/optimizing/workflow.md 3 KB
  • workflows/optimizing/steps/step-01-init.md 3 KB
  • workflows/optimizing/steps/step-01b-continue.md 2 KB
  • workflows/optimizing/steps/step-02-identify-opportunities.md 4 KB
  • workflows/optimizing/steps/step-03-optimize-setup.md 4 KB
  • workflows/optimizing/steps/step-04-fix-open-handles.md 6 KB
  • workflows/optimizing/steps/step-05-optimize-async.md 4 KB
  • workflows/optimizing/steps/step-06-optimize-data.md 3 KB
  • workflows/optimizing/steps/step-07-optimize-organization.md 3 KB
  • workflows/optimizing/steps/step-08-measure-improvement.md 3 KB
  • workflows/optimizing/steps/step-09-document-optimizations.md 4 KB
  • workflows/optimizing/templates/output-template.md 1 KB
  • workflows/reviewing/workflow.md 2 KB
  • workflows/reviewing/steps/step-01-init.md 2 KB
  • workflows/reviewing/steps/step-01b-continue.md 2 KB
  • workflows/reviewing/steps/step-02-test-structure.md 2 KB
  • workflows/reviewing/steps/step-03-setup-teardown.md 2 KB
  • workflows/reviewing/steps/step-04-aaa-pattern.md 2 KB
  • workflows/reviewing/steps/step-05-assertions.md 3 KB
  • workflows/reviewing/steps/step-06-coverage.md 2 KB
  • workflows/reviewing/steps/step-07-exception-testing.md 3 KB
  • workflows/reviewing/steps/step-08-compile-report.md 4 KB
  • workflows/reviewing/templates/output-template.md 1 KB
  • workflows/running/workflow.md 2 KB
  • workflows/running/steps/step-01-init.md 3 KB
  • workflows/running/steps/step-01b-continue.md 2 KB
  • workflows/running/steps/step-02-run-tests.md 2 KB
  • workflows/running/steps/step-03-interpret-results.md 2 KB
  • workflows/running/steps/step-04-coverage.md 2 KB
  • workflows/running/steps/step-05-handle-scenarios.md 3 KB
  • workflows/running/steps/step-06-summary-report.md 3 KB
  • workflows/running/templates/output-template.md 1 KB
  • workflows/setup/workflow.md 2 KB
  • workflows/setup/steps/step-01-init.md 3 KB
  • workflows/setup/steps/step-01b-continue.md 2 KB
  • workflows/setup/steps/step-02-dependencies.md 2 KB
  • workflows/setup/steps/step-03-configure-jest.md 2 KB
  • workflows/setup/steps/step-04-test-helpers.md 2 KB
  • workflows/setup/steps/step-05-verify-setup.md 3 KB
  • workflows/setup/steps/step-06-npm-scripts.md 3 KB
  • workflows/setup/templates/output-template.md 1 KB
  • workflows/writing/workflow.md 2 KB
  • workflows/writing/steps/step-01-init.md 3 KB
  • workflows/writing/steps/step-01b-continue.md 2 KB
  • workflows/writing/steps/step-02-test-file-structure.md 3 KB
  • workflows/writing/steps/step-03-plan-test-cases.md 2 KB
  • workflows/writing/steps/step-04-happy-path.md 2 KB
  • workflows/writing/steps/step-05-edge-cases.md 3 KB
  • workflows/writing/steps/step-06-error-cases.md 3 KB
  • workflows/writing/steps/step-07-business-rules.md 3 KB
  • workflows/writing/steps/step-08-verify-mocks.md 4 KB
  • workflows/writing/templates/output-template.md 1 KB

Wersje

W bazie nie ma innych skilli o nazwie typescript-unit-testing.

Migawka bazy z 28.10.2026 · wersja paczki 81d3aefd0da3 (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100javascript-unit-testing
    Pisze wysokiej jakości testy jednostkowe w JavaScript i TypeScript z Jest: wzorzec AAA, stuby i mocki, kod asynchroniczny, unikanie niestabilnych testów i TDD.
  • 100typescript-testing
    Pisze testy TypeScript w Jest lub Vitest: testy jednostkowe i integracyjne, mocki oraz pokrycie kodu.
  • 100nestjs-testing
    Pisze testy jednostkowe i E2E w NestJS z Jest i supertest, wraz ze strategiami mockowania i izolacją bazy danych.
  • 100typescript-e2e-testing
    Prowadzi testy E2E i integracyjne projektów TypeScript i NestJS z Jest, supertest i prawdziwą infrastrukturą w Docker (Kafka, PostgreSQL, MongoDB, Redis) według wzorca Given-When-Then.
  • 100react-testing
    Pisze i przegląda testy React i TypeScript dla frontendu Sentry z Jest i React Testing Library, w tym mockowanie API przez MockApiClient i naprawa niestabilnych testów.
  • 100jest-skill
    Generuje testy jednostkowe i integracyjne w Jest w JavaScript lub TypeScript: mockowanie, snapshoty, testy asynchroniczne i testy komponentów React.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.