javascript-testing-patterns

Implement unit, integration, and component tests using Jest, Vitest, and Testing Library.

1|Updated Aug 17, 2025
One-click install
npx skills add https://github.com/ratnesh-maurya/mdconverter --skill javascript-testing-patterns-ratnesh-maurya
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: javascript-testing-patterns
Source: https://github.com/ratnesh-maurya/mdconverter/tree/main/.claude/skills/javascript-testing-patterns
Command: npx skills add https://github.com/ratnesh-maurya/mdconverter --skill javascript-testing-patterns-ratnesh-maurya

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing reliable tests for JavaScript and TypeScript applications requires knowing framework configuration, mocking strategies, and testing patterns, which developers often implement inconsistently or skip entirely. ## Core Features & Use Cases - Framework Setup: Provides ready-to-use Jest and Vitest configurations with coverage thresholds, setup files, and TypeScript support. - Testing Patterns: Covers unit tests for functions and classes, async function testing, module mocking, dependency injection, and spying on functions. - Integration & Component Testing: Includes API integration tests with supertest, database repository tests, React component tests with Testing Library, and custom hook testing. - Use Case: When building a new Express API, use this Skill to scaffold integration tests that spin up a test database, seed fixtures, and verify endpoint behavior including authentication flows. ## Quick Start Ask the assistant to write Vitest unit tests with mocked dependencies for a specific service class in your project.

Frequently Asked Questions about javascript-testing-patterns

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

FAQPage Schema
How do I write unit tests for async functions in JavaScript?

Use async/await in your test functions and mock fetch or other async dependencies with vi.fn() or jest.fn(). Assert resolved values with expect and test rejections using expect().rejects.toThrow().

Jest vs Vitest: which testing framework should I use?

Vitest offers faster execution and native Vite integration with a Jest-compatible API, making it ideal for Vite-based projects. Jest remains a full-featured choice with broad ecosystem support, especially for older or non-Vite codebases.

How do I mock modules in Vitest tests?

Use vi.mock() with a factory function that returns mocked implementations of the module's exports. For example, mock nodemailer by returning an object whose createTransport returns a stubbed sendMail function.

How do I test React components with Testing Library?

Render the component with render(), query elements using semantic queries like getByRole or getByPlaceholderText, and simulate user interactions with fireEvent. Assert on rendered output and callback invocations rather than implementation details.

How do I test code that uses timers or setTimeout?

Use vi.useFakeTimers() to replace real timers, then call vi.advanceTimersByTime() to simulate time passing synchronously. Restore real timers with vi.useRealTimers() after the test to avoid side effects.

What test coverage threshold should I aim for?

The configurations in this Skill set an 80% threshold for branches, functions, lines, and statements. This is a common baseline that catches most regressions without making tests brittle or slowing development.