From harness-claude
Writes focused, isolated unit tests using AAA (Arrange-Act-Assert) pattern with describe/it/expect for functions, classes, modules. For TDD, pure logic verification, fast suites.
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Write focused, isolated unit tests using AAA pattern with describe/it/expect
Provides strategies for writing maintainable unit, integration, and E2E tests using AAA pattern, mocking, test naming, and organization. Useful for TDD and test infrastructure.
Provides Jest, Vitest, and Testing Library patterns for unit, integration, E2E testing in JavaScript/TypeScript apps, including mocking, fixtures, and TDD workflows.
Provides unit testing patterns for isolated business logic: AAA structure, parametrized tests (test.each, @pytest.mark.parametrize), fixture scoping, MSW/VCR mocking, factories (FactoryBoy, faker-js). Covers Vitest, Jest, pytest.
Share bugs, ideas, or general feedback.
Write focused, isolated unit tests using AAA pattern with describe/it/expect
import { describe, it, expect } from 'vitest';
import { calculateDiscount } from './pricing';
describe('calculateDiscount', () => {
it('applies 10% discount for orders over $100', () => {
// Arrange
const orderTotal = 150;
const discountRate = 0.1;
// Act
const result = calculateDiscount(orderTotal, discountRate);
// Assert
expect(result).toBe(15);
});
});
it('returns the created user with all fields', () => {
const user = createUser({ name: 'Alice', email: 'alice@test.com' });
expect(user).toMatchObject({
name: 'Alice',
email: 'alice@test.com',
});
expect(user.id).toBeDefined();
expect(user.createdAt).toBeInstanceOf(Date);
});
describe blocks to group related tests:describe('UserService', () => {
describe('create', () => {
it('creates a user with valid input', () => {
/* ... */
});
it('throws on duplicate email', () => {
/* ... */
});
it('normalizes email to lowercase', () => {
/* ... */
});
});
describe('findById', () => {
it('returns the user when found', () => {
/* ... */
});
it('returns null when not found', () => {
/* ... */
});
});
});
describe('parseAge', () => {
it('parses valid integer strings', () => {
expect(parseAge('25')).toBe(25);
});
it('returns null for empty string', () => {
expect(parseAge('')).toBeNull();
});
it('returns null for non-numeric input', () => {
expect(parseAge('abc')).toBeNull();
});
it('returns null for negative numbers', () => {
expect(parseAge('-5')).toBeNull();
});
it('returns null for floating point', () => {
expect(parseAge('25.5')).toBeNull();
});
it('handles zero', () => {
expect(parseAge('0')).toBe(0);
});
});
toThrow:it('throws on invalid email', () => {
expect(() => validateEmail('not-an-email')).toThrow('Invalid email');
});
it('throws specific error type', () => {
expect(() => validateEmail('')).toThrow(ValidationError);
});
beforeEach/afterEach for shared setup:describe('CartService', () => {
let cart: CartService;
beforeEach(() => {
cart = new CartService();
});
it('starts empty', () => {
expect(cart.items).toHaveLength(0);
});
it('adds items', () => {
cart.add({ id: '1', qty: 2 });
expect(cart.items).toHaveLength(1);
});
});
it('resolves with user data', async () => {
const user = await getUser('123');
expect(user.name).toBe('Alice');
});
it('rejects with not found error', async () => {
await expect(getUser('nonexistent')).rejects.toThrow('User not found');
});
// Good: describes behavior
it('applies free shipping for orders over $50', () => {
/* ... */
});
// Bad: describes implementation
it('calls calculateShipping with zero', () => {
/* ... */
});
Unit tests verify individual units of code (functions, classes, modules) in isolation from external dependencies. They should be fast (< 10ms each), deterministic, and independent of execution order.
What makes a good unit test:
Test file organization: Place test files next to the source file (user.ts → user.test.ts) or in a parallel __tests__ directory. Co-location makes it easy to find tests and keeps imports short.
expect matcher selection:
toBe — strict equality (===). Use for primitivestoEqual — deep equality. Use for objects and arraystoMatchObject — partial deep equality. Object under test may have extra propertiestoContain — array includes element or string includes substringtoHaveLength — array or string lengthtoBeGreaterThan / toBeLessThan — numeric comparisonstoThrow — function throws. Wrap the call in an arrow functionTrade-offs:
toMatchObject is flexible — but can pass when extra unexpected properties exist