javascript-testing-patterns

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

Updated Apr 23, 2026
One-click install
npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill javascript-testing-patterns-sanketadlak
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: javascript-testing-patterns
Source: https://github.com/SanketAdlak/PDMProjectDesign/tree/main/.agents/skills/javascript-testing-patterns
Command: npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill javascript-testing-patterns-sanketadlak

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing reliable JavaScript/TypeScript tests requires knowing framework configuration, mocking strategies, async handling, and component testing patterns, which developers often implement inconsistently or skip entirely. ## Core Features & Use Cases - Framework Setup: Ready-to-use Jest and Vitest configurations with coverage thresholds, setup files, and TypeScript support. - Testing Patterns: Concrete examples for pure functions, classes, async services, module mocking, dependency injection, and spies. - Integration & Frontend Testing: Supertest API tests, PostgreSQL repository tests, and React component/hook tests with Testing Library. - Use Case: When building a new Express API, use this Skill to scaffold Vitest configuration, write unit tests for services with mocked repositories, and add supertest integration tests covering authentication and error responses. ## Quick Start Write Vitest unit tests with mocked dependencies for my UserService class and set up the test configuration.

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 in TypeScript with Vitest?

Vitest unit tests use describe/it blocks with expect assertions, importing functions directly from source files. Configure vitest.config.ts with globals, environment, and coverage settings, then test pure functions, classes, and async code with mockResolvedValue and 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 modern Vite projects. Jest with ts-jest remains a full-featured choice for established projects; both support coverage thresholds, mocking, and setup files.

How do I mock fetch API calls in JavaScript tests?

Assign global.fetch = vi.fn() and use mockResolvedValueOnce to return fake responses with ok status and json methods. Then assert fetch was called with the expected URL and request options like method and body.

How do I test React components with Testing Library?

Render the component with render(), query elements using getByRole, getByPlaceholderText, or getByTestId, and simulate events with fireEvent. For hooks, use renderHook with act() to trigger state updates and assert on result.current values.

How do I write integration tests for an Express API with a database?

Use supertest to send HTTP requests against the app and a real PostgreSQL test database via pg Pool. Create tables in beforeAll, truncate data in beforeEach for isolation, and drop tables plus end the pool in afterAll.

Why do my tests fail when code uses setTimeout or timers?

Real timers make tests slow and flaky. Use vi.useFakeTimers() to control time, then vi.advanceTimersByTime() to trigger scheduled callbacks deterministically, and restore with vi.useRealTimers() after the test.