javascript-unit-testing

Write maintainable Jest unit tests for JavaScript and TypeScript projects.

12|2|Updated Mar 11, 2025
One-click install
npx skills add https://github.com/el-feo/ai-context --skill javascript-unit-testing
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: javascript-unit-testing
Source: https://github.com/el-feo/ai-context/tree/main/skills/javascript-unit-testing
Command: npx skills add https://github.com/el-feo/ai-context --skill javascript-unit-testing

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides comprehensive guidance for crafting maintainable JavaScript/TypeScript unit tests using Jest, with patterns for mocks, stubs, DI, async testing, and test quality.

Core Features & Use Cases

  • Clear patterns for AAA testing, mocks, stubs, and dependency injection.
  • Async testing techniques, timers, and test structure best practices.
  • Guidance for test quality, refactoring, and TDD workflows.

Quick Start

Begin by writing small unit tests with the AAA pattern and run tests with npm test or yarn test.

Frequently Asked Questions about javascript-unit-testing

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

FAQPage Schema
How do I write unit tests for JavaScript with Jest?

Unit tests for JavaScript use Jest's test framework with the AAA pattern: Arrange setup, Act on code, Assert results. Write small, focused tests that verify single behaviors, run with npm test or yarn test, and keep tests isolated and fast by mocking external dependencies.

What's the best way to mock dependencies in Jest tests?

Mock dependencies using stubs, mocks, and dependency injection to isolate the code under test. Jest provides built-in mocking utilities; inject dependencies as parameters or use constructor injection so tests control what the code receives, avoiding real API calls or database hits.

How do I test async code and timers in JavaScript?

Async testing in Jest uses async/await, Promises, or callbacks with done() parameter. For timers, use fake timers with jest.useFakeTimers() to control time advancement, then jest.runAllTimers() or jest.advanceTimersByTime() to trigger async behavior without delays in tests.

Can I use these testing patterns with TypeScript?

Yes, Jest testing patterns work directly with TypeScript projects. The same AAA structure, mocking strategies, and async techniques apply; Jest handles TypeScript compilation automatically with proper configuration, so you write type-safe tests with the same maintainability benefits.

Why are my Jest tests flaky or fail inconsistently?

Flaky tests often stem from incomplete mocking, untamed async code, or testing implementation details instead of behavior. Ensure all external dependencies are mocked, async operations complete before assertions, and tests verify outcomes users care about, not internal state or timing-dependent behavior.

How do I refactor tests for better maintainability?

Refactor tests by extracting setup into helper functions, naming tests clearly with intent, using consistent patterns like AAA across the suite, and removing redundant assertions. Group related tests, isolate each test fully, and keep the test readable so intent is obvious without comments.