klh-testing-patterns

Provides Jest testing patterns, factory functions, and TDD workflow guidance for unit tests.

1|Updated Apr 16, 2026
One-click install
npx skills add https://github.com/klh/skills --skill klh-testing-patterns-klh
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: klh-testing-patterns
Source: https://github.com/klh/skills/tree/main/.well-known/agent-skills/klh-testing-patterns
Command: npx skills add https://github.com/klh/skills --skill klh-testing-patterns-klh

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing consistent, maintainable unit tests is hard: test data gets duplicated, mocks are misused, and teams skip the TDD red-green-refactor discipline. This Skill provides proven Jest testing patterns, factory functions, and mocking strategies so tests stay DRY, behavior-focused, and reliable. ## Core Features & Use Cases - Factory Pattern: Create getMockX(overrides) functions for props and data with sensible defaults, keeping tests DRY and consistent. - Mocking Strategies: Patterns for mocking modules, GraphQL hooks, and functions with jest.mock and jest.requireMock. - TDD RED Checkpoint: Enforces writing a failing test first and verifying it fails cleanly before any implementation. - Use Case: When building a React Native component, use the custom render utility with theme providers, generate props via a factory, and structure tests with describe blocks for rendering, interactions, and edge cases. ## Quick Start Ask the agent to write Jest unit tests for your component using factory functions and the TDD red-green-refactor workflow.

Frequently Asked Questions about klh-testing-patterns

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

FAQPage Schema
How do I write Jest unit tests with factory functions?

Create a getMockX function that returns default values and accepts a Partial overrides object, then spread overrides last. This keeps test data DRY and consistent across tests while allowing per-test customization of specific properties.

How to mock a module or GraphQL hook in Jest?

Use jest.mock with the module path, optionally passing a factory that returns jest.fn() implementations. Access the mock via jest.requireMock and set return values with mockReturnValue inside each test.

What is the TDD red-green-refactor cycle in Jest?

Write a failing test first, run it to confirm it fails cleanly with a meaningful message, then write the minimal implementation to pass, and refactor after green. A test that passes before implementation indicates a broken assertion.

Does this testing approach work with React Native components?

Yes, the patterns use @testing-library/react-native with a custom render function that wraps components in required providers like ThemeProvider. Queries, fireEvent, and waitFor handle rendering, interaction, and async assertions.

Why should I avoid testing mock behavior instead of real behavior?

Asserting that a mock was called only verifies the mock, not the actual behavior users see. Prefer assertions on rendered output, such as checking that expected text appears on screen, to catch real bugs.