Testing Test Writing

Automate Jest unit and integration tests for TypeScript NestJS projects.

1|Updated Dec 4, 2024
One-click install
npx skills add https://github.com/imkdw/imkdw-dev --skill testing-test-writing-imkdw
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Testing Test Writing
Source: https://github.com/imkdw/imkdw-dev/tree/main/.claude/skills/testing-test-writing
Command: npx skills add https://github.com/imkdw/imkdw-dev --skill testing-test-writing-imkdw

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Inadequate or poorly written tests lead to undetected bugs, fragile code, and a lack of confidence in deployments, increasing the risk of production issues. This Skill guides the creation of robust unit and integration tests, ensuring code reliability.

Core Features & Use Cases

  • Unit & Integration Testing: Differentiate and implement tests for isolated components (unit) and full system flows with database interactions (integration).
  • Jest Framework: Utilize Jest for efficient test execution, powerful mocking capabilities, and clear assertion syntax.
  • Behavior-Driven Testing: Focus tests on expected behavior rather than internal implementation details, making tests more resilient to refactoring.
  • Use Case: When developing a new CreateArticleUseCase, write an integration test that verifies an article is successfully created with valid data and that an ExistArticleException is thrown for duplicate titles, using a real database transaction.

Quick Start

Help me write a unit test for a CreateUserDto to ensure it fails validation when the email field is empty, using class-validator and Jest's expect assertions.

Frequently Asked Questions about Testing Test Writing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write unit tests for TypeScript DTOs and entities with Jest?

Unit tests for DTOs and entities validate individual components in isolation using Jest's expect assertions. Create *.spec.ts files in your test directory, import the DTO or entity class, instantiate it with test data, and assert on expected behavior—for example, verifying that class-validator decorators reject invalid input like empty email fields.

What's the difference between unit tests and integration tests in a NestJS project?

Unit tests validate isolated components like DTOs or domain entities without external dependencies. Integration tests verify complete workflows using real database transactions, testing use cases and queries end-to-end. Both use Jest; integration tests exercise the full system including database persistence.

How do I mock external dependencies in Jest tests?

Jest provides built-in mocking capabilities to isolate components from external services. Use jest.mock() to replace dependencies, or manually create mock objects that return predictable values. This lets unit tests run fast and predictably without calling real APIs or databases.

How do I test behavior instead of implementation details?

Behavior-driven testing focuses on what the code should do—its observable outcome—rather than how it's coded internally. Test the expected result (e.g., article created, exception thrown for duplicates) not the specific function calls or internal state. This makes tests resilient to refactoring.

Can I use Jest to test both frontend and backend TypeScript code?

Jest works across TypeScript frontends and backends. This Skill applies Jest to NestJS backend projects for unit and integration testing, but Jest's test patterns—setup, mocking, assertions—are framework-agnostic and apply equally to frontend components.

What test patterns should I follow to ensure code quality?

Establish patterns: unit tests for DTOs and entities, integration tests with real database transactions for use cases, descriptive test naming, test fixtures in test/fixtures, and mocked external dependencies. Consistent patterns enforce reliability and make tests maintainable across your project.