testing-patterns

Write Jest unit tests using factory functions, mocking strategies, and TDD workflow.

1|Updated Aug 4, 2026
One-click install
npx skills add https://github.com/Hamzaoui-Louai/LEAN-gym-dashboard-frontend --skill testing-patterns-hamzaoui-louai
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: testing-patterns
Source: https://github.com/Hamzaoui-Louai/LEAN-gym-dashboard-frontend/tree/main/.opencode/skills/testing-patterns
Command: npx skills add https://github.com/Hamzaoui-Louai/LEAN-gym-dashboard-frontend --skill testing-patterns-hamzaoui-louai

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing consistent, maintainable unit tests is hard when test data is duplicated, mocks are inconsistent, and tests verify implementation details instead of behavior. This Skill provides proven Jest testing patterns that keep test suites DRY, behavior-focused, and aligned with the TDD red-green-refactor cycle. ## Core Features & Use Cases - Factory Functions: Create reusable getMockX(overrides) factories for component props and data models with sensible defaults and partial overrides. - Mocking Strategies: Mock entire modules, GraphQL hooks, and utilities with jest.mock factory functions and jest.requireMock access patterns. - TDD Workflow: Follow the red-green-refactor cycle with structured describe blocks covering rendering, user interactions, and edge cases. - Use Case: When adding a new React component, generate a test file with a props factory, a custom render wrapper with theme providers, and behavior-focused assertions for loading, error, and interaction states. ## Quick Start Write Jest unit tests for my React component using factory functions, a custom render wrapper, and behavior-focused assertions.

Frequently Asked Questions about testing-patterns

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

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

Create a getMockX function that returns default values and spreads a Partial overrides parameter, such as getMockUser({ role: 'admin' }). This keeps test data consistent, DRY, and easy to customize per test case.

How to mock a module in Jest with a factory function?

Use jest.mock with a factory argument, for example jest.mock('utils/analytics', () => ({ Analytics: { logEvent: jest.fn() } })). Access the mock in tests via jest.requireMock to configure return values or assert calls.

How do I mock GraphQL hooks in Jest tests?

Mock the generated hooks file with jest.mock('./GetItems.generated', () => ({ useGetItemsQuery: jest.fn() })), then use mockReturnValue to supply data, loading, and error states for each test scenario.

What is the difference between getBy, queryBy, and findBy in Testing Library?

Use getBy when the element must exist, queryBy when asserting an element is absent (expect null), and findBy with waitFor for elements that appear asynchronously after data loads.

Why should tests avoid asserting on mock function calls?

Asserting that a mock was called tests the mock rather than real behavior, creating brittle tests tied to implementation. Instead, assert on observable outcomes like rendered text or state changes visible to users.

When should I not use the factory pattern in tests?

Factories add indirection that may be unnecessary for trivial one-off values in a single test. They deliver the most value when the same data shape appears across multiple tests or requires consistent defaults.