Loading...
Loading...
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".
npx skill4agent add asyrafhussin/agent-skills testing-best-practices| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Test Structure | CRITICAL | |
| 2 | Test Isolation | CRITICAL | |
| 3 | Assertions | HIGH | |
| 4 | Test Data | HIGH | |
| 5 | Mocking | MEDIUM | |
| 6 | Coverage | MEDIUM | |
| 7 | Performance | LOW | |
struct-aaastruct-namingstruct-one-assertstruct-describe-itstruct-given-when-theniso-independentiso-no-shared-stateiso-deterministiciso-no-order-dependencyiso-cleanupassert-specificassert-meaningful-messagesassert-expected-firstassert-no-magic-numbersdata-factoriesdata-minimaldata-realisticdata-fixturesmock-only-boundariesmock-verify-interactionsmock-minimalmock-realisticcov-meaningfulcov-edge-casescov-unhappy-pathscov-not-100-percentperf-fast-unitperf-slow-integrationperf-parallelrules/it('calculates total with discount', () => {
// Arrange - set up test data
const cart = new ShoppingCart();
cart.addItem({ name: 'Book', price: 20 });
cart.applyDiscount(0.1);
// Act - execute the code under test
const total = cart.getTotal();
// Assert - verify the result
expect(total).toBe(18);
});// ✅ 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', () => {});
});
// ❌ Vague names
it('test1', () => {});
it('should work', () => {});// ✅ Each test sets up its own data
beforeEach(() => {
mockRepository = { save: jest.fn(), find: jest.fn() };
service = new OrderService(mockRepository);
});
// ❌ Shared state between tests
let globalOrder: Order; // Tests depend on each other /\
/ \ E2E Tests (few)
/----\ - Test critical user flows
/ \ - Slow, expensive
/--------\ Integration Tests (some)
/ \ - Test component interactions
/------------\ - Database, API calls
/ \ Unit Tests (many)
/----------------\ - Fast, isolated
- Test single unitsfile:line - [category] Description of issuetests/user.test.ts:15 - [struct] Missing Arrange/Act/Assert separation
tests/order.test.ts:42 - [iso] Test depends on previous test's state
tests/cart.test.ts:28 - [assert] Multiple unrelated assertions in one testrules/struct-aaa-pattern.md
rules/iso-independent-tests.md
rules/data-factories.mdrules/