Write effective unit tests that are fast, isolated, readable, and maintainable following industry best practices and AAA (Arrange-Act-Assert) pattern.
When to Use
Writing tests for new code
Improving test coverage
Establishing testing standards
Refactoring with test safety
Implementing TDD (Test-Driven Development)
Creating test utilities and mocks
Quick Start
Minimal working example:
// Jest/JavaScript example
describe("UserService", () => {
describe("createUser", () => {
it("should create user with valid data", async () => {
// Arrange - Set up test data and dependencies
const userData = {
email: "john@example.com",
firstName: "John",
lastName: "Doe",
};
const mockDatabase = createMockDatabase();
const service = new UserService(mockDatabase);
// Act - Execute the function being tested
const result = await service.createUser(userData);
// Assert - Verify the outcome
expect(result.id).toBeDefined();
expect(result.email).toBe("john@example.com");
expect(mockDatabase.save).toHaveBeenCalledWith(
expect.objectContaining(userData),
);
});
});
});
Reference Guides
Detailed implementations in the references/ directory:
Guide
Contents
Test Structure (AAA Pattern)
Test Structure (AAA Pattern)
Test Cases by Language
Test Cases by Language
Mocking & Test Doubles
Mocking & Test Doubles
Testing Async Code
Testing Async Code, Test Coverage
Testing Edge Cases
Testing Edge Cases
Example: Complete Test Suite
import { UserService } from "./user-service";
Best Practices
✅ DO
Write tests before or alongside code (TDD)
Test one thing per test
Use descriptive test names
Follow AAA pattern
Test edge cases and error conditions
Keep tests isolated and independent
Use setup/teardown appropriately
Mock external dependencies
Aim for high coverage on critical paths
Make tests fast (< 10ms each)
Use parameterized tests for similar cases
Test public interfaces, not implementation
❌ DON'T
Test implementation details
Write tests that depend on each other
Ignore failing tests
Test third-party library code
Use real databases/APIs in unit tests
Make tests too complex
Skip edge cases
Forget to clean up resources
Test everything (focus on business logic)
Write flaky tests
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: aj-geddes-useful-ai-prompts-unit-testing-framework3description: Unit Testing Framework4---56# Unit Testing Framework78## Table of Contents910- [Overview](#overview)11- [When to Use](#when-to-use)12- [Quick Start](#quick-start)13- [Reference Guides](#reference-guides)14- [Best Practices](#best-practices)1516## Overview1718Write effective unit tests that are fast, isolated, readable, and maintainable following industry best practices and AAA (Arrange-Act-Assert) pattern.1920## When to Use2122- Writing tests for new code23- Improving test coverage24- Establishing testing standards25- Refactoring with test safety26- Implementing TDD (Test-Driven Development)27- Creating test utilities and mocks2829## Quick Start3031Minimal working example:3233```javascript34// Jest/JavaScript example35describe("UserService", () => {36 describe("createUser", () => {37 it("should create user with valid data", async () => {38 // Arrange - Set up test data and dependencies39 const userData = {40 email: "john@example.com",41 firstName: "John",42 lastName: "Doe",43 };44 const mockDatabase = createMockDatabase();45 const service = new UserService(mockDatabase);4647 // Act - Execute the function being tested48 const result = await service.createUser(userData);4950 // Assert - Verify the outcome51 expect(result.id).toBeDefined();52 expect(result.email).toBe("john@example.com");53 expect(mockDatabase.save).toHaveBeenCalledWith(54 expect.objectContaining(userData),55 );56 });57 });58});59```6061## Reference Guides6263Detailed implementations in the `references/` directory:6465| Guide | Contents |66|---|---|67| [Test Structure (AAA Pattern)](references/test-structure-aaa-pattern.md) | Test Structure (AAA Pattern) |68| [Test Cases by Language](references/test-cases-by-language.md) | Test Cases by Language |69| [Mocking & Test Doubles](references/mocking-test-doubles.md) | Mocking & Test Doubles |70| [Testing Async Code](references/testing-async-code.md) | Testing Async Code, Test Coverage |71| [Testing Edge Cases](references/testing-edge-cases.md) | Testing Edge Cases |72| [Example: Complete Test Suite](references/example-complete-test-suite.md) | import { UserService } from "./user-service"; |7374## Best Practices7576### ✅ DO7778- Write tests before or alongside code (TDD)79- Test one thing per test80- Use descriptive test names81- Follow AAA pattern82- Test edge cases and error conditions83- Keep tests isolated and independent84- Use setup/teardown appropriately85- Mock external dependencies86- Aim for high coverage on critical paths87- Make tests fast (< 10ms each)88- Use parameterized tests for similar cases89- Test public interfaces, not implementation9091### ❌ DON'T9293- Test implementation details94- Write tests that depend on each other95- Ignore failing tests96- Test third-party library code97- Use real databases/APIs in unit tests98- Make tests too complex99- Skip edge cases100- Forget to clean up resources101- Test everything (focus on business logic)102- Write flaky tests103104---105> Converted and distributed by [TomeVault](https://tomevault.io/claim/aj-geddes) — claim your Tome and manage your conversions.106<!-- tomevault:4.0:skill_md:2026-04-11 -->
Run npx skillmds@latest add tomevault-io/aj-geddes-useful-ai-prompts-unit-testing-framework in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Unit Testing Framework It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
tomevault-io (@tomevault-io) published this skill. Their other Agent Skills are listed on their SkillMD profile.