Testing best practices

From AsyrafHussin/agent-skills · View interactive version

Testing best practices is an AI agent skill from the GitHub repository AsyrafHussin/agent-skills. 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".

How to use this skill

Skill file

Testing Best Practices

Unit testing, integration testing, and TDD principles for reliable, maintainable test suites. Contains 34 rules across 7 categories using TypeScript with Jest/Vitest.

Metadata

When to Apply

Reference these guidelines when:

Rule Categories by Priority

| Priority | Category | Impact | Prefix |

|----------|----------|--------|--------|

| 1 | Test Structure | CRITICAL | struct- |

| 2 | Test Isolation | CRITICAL | iso- |

| 3 | Assertions | HIGH | assert- |

| 4 | Test Data | HIGH | data- |

| 5 | Mocking | MEDIUM | mock- |

| 6 | Coverage | MEDIUM | cov- |

| 7 | Performance | LOW | perf- |

Quick Reference

1. Test Structure (CRITICAL)

2. Test Isolation (CRITICAL)

3. Assertions (HIGH)

4. Test Data (HIGH)

5. Mocking (MEDIUM)

6. Coverage (MEDIUM)

7. Performance (LOW)

Essential Guidelines

AAA Pattern (Arrange, Act, Assert)

it('calculates total with discount', () => {
  // Arrange
  const cart = new ShoppingCart();
  cart.addItem({ name: 'Book', price: 20 });
  cart.applyDiscount(0.1);

  // Act
  const total = cart.getTotal();

  // Assert
  expect(total).toBe(18);
});

Descriptive Test Names

// ✅ Describes behavior and scenario
describe('UserService.register', () => {
  it('creates user with hashed password', () => {});
  it('throws ValidationError when email is invalid', () => {});
  it('sends welcome email after successful registration', () => {});
});

Test Isolation

// ✅ Each test sets up its own data
beforeEach(() => {
  mockRepository = { save: vi.fn(), find: vi.fn() };
  service = new OrderService(mockRepository);
});

How to Use

Read individual rule files for detailed explanations:

rules/struct-aaa-pattern.md
rules/iso-independent-tests.md
rules/mock-boundaries.md
rules/cov-meaningful.md

References

Full Compiled Document

For the complete guide with all rules expanded: AGENTS.md

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.