All skills
asyrafhussin avatar

/testing-best-practices

@575c801

Unit testing, integration testing, and test-driven development principles. Use when writing tests, reviewing test code, improving test coverage, or setting up testing strategy. Triggers on "write tests", "review tests", "testing best practices", or "TDD".

Use this Skill: https://skilld.dev/gh/asyrafhussin/agent-skills/testing-best-practices

This session only. Nothing lands on disk.

rulesmock-boundaries.md

≈835 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Mock Only at Boundaries

Impact: MEDIUM (reduces brittle tests by 60%, improves refactoring confidence)

Mock external dependencies at system boundaries — APIs, databases, file system — but keep business logic unmocked so tests verify real behavior.

Incorrect

// ❌ Bad: mocking internal functions and private methods
import { calculateDiscount } from './pricing';
import { formatCurrency } from './utils';

vi.mock('./pricing', () => ({
  calculateDiscount: vi.fn().mockReturnValue(10),
}));

vi.mock('./utils', () => ({
  formatCurrency: vi.fn().mockReturnValue('$90.00'),
}));

describe('OrderService', () => {
  test('applies discount to order', () => {
    const order = new OrderService();
    const result = order.processOrder({ price: 100, discountCode: 'SAVE10' });

    // Testing wiring, not behavior
    expect(calculateDiscount).toHaveBeenCalledWith('SAVE10', 100);
    expect(formatCurrency).toHaveBeenCalledWith(90);
    expect(result.formattedTotal).toBe('$90.00');
  });
});

Problems:

  • Mocking internal utility functions couples tests to implementation details
  • Refactoring internals (e.g., inlining formatCurrency) breaks the test even if behavior is unchanged
  • Tests verify wiring, not actual business logic
  • False confidence — mocks return what you told them to, not what the real code does

Correct

// ✅ Good: mock only external boundaries, test real business logic
import { OrderService } from './order-service';

// Mock the external HTTP client (boundary)
const mockPaymentGateway = {
  charge: vi.fn(),
};

// Mock the external database (boundary)
const mockOrderRepository = {
  save: vi.fn(),
};

describe('OrderService', () => {
  const service = new OrderService(mockPaymentGateway, mockOrderRepository);

  beforeEach(() => {
    vi.clearAllMocks();
  });

  test('applies discount and charges correct amount', async () => {
    mockPaymentGateway.charge.mockResolvedValue({ id: 'txn_123', status: 'success' });
    mockOrderRepository.save.mockResolvedValue({ id: 'order_456' });

    const result = await service.processOrder({
      price: 100,
      discountCode: 'SAVE10',
    });

    // Real discount calculation and formatting run — only external calls are mocked
    expect(result.total).toBe(90);
    expect(result.formattedTotal).toBe('$90.00');
    expect(mockPaymentGateway.charge).toHaveBeenCalledWith(90);
  });

  test('rejects order when payment fails', async () => {
    mockPaymentGateway.charge.mockRejectedValue(new Error('Card declined'));

    await expect(
      service.processOrder({ price: 50, discountCode: '' })
    ).rejects.toThrow('Card declined');

    expect(mockOrderRepository.save).not.toHaveBeenCalled();
  });
});

Benefits:

  • Business logic (discount calculation, formatting) runs for real and is actually tested
  • Tests survive internal refactoring — only boundary contracts matter
  • Failures reveal genuine bugs, not outdated mock wiring
  • External side effects (payments, persistence) are safely isolated

Reference: Mock Only What You Own

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides comprehensive guidelines and best practices for unit testing, integration testing, and Test-Driven Development (TDD). It consists entirely of documentation and illustrative code examples using industry-standard tools like Jest and Vitest. No security risks or malicious patterns were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    29 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 575c801. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago
metadata
{
  "author": "agent-skills",
  "version": "2.0.0"
}

README badge

README badge for asyrafhussin/agent-skills/testing-best-practices