writing-tests
Principles for writing effective, maintainable tests. Covers naming conventions, assertion best practices, and comprehensive edge case checklists. Based on BugMagnet by Gojko Adzic.
What this skill does
# Writing Tests
How to write tests that catch bugs, document behavior, and remain maintainable.
> Based on [BugMagnet](https://github.com/gojko/bugmagnet-ai-assistant) by Gojko Adzic. Adapted with attribution and aligned with fn(args, deps) and Result type patterns.
## Core Principle
Test names describe outcomes, not actions. Assertions match test titles. Specific assertions catch specific bugs. One concept per test.
## Critical Rules
π¨ **Test names describe outcomes, not actions.** "returns empty array when input is null" not "test null input". The name IS the specification.
π¨ **Assertions must match test titles.** If the test claims to verify "different IDs", assert on the actual ID valuesβnot just count or existence.
π¨ **Assert specific values, not types.** `expect(result.value).toEqual(['First.', ' Second.'])` not `expect(result).toBeDefined()`. Specific assertions catch specific bugs.
π¨ **One concept per test.** Each test verifies one behavior. If you need "and" in your test name, split it.
π¨ **Bugs cluster together.** When you find one bug, test related scenarios. The same misunderstanding often causes multiple failures.
## When This Applies
- Writing new tests (especially during TDD RED phase)
- Reviewing test quality
- Expanding test coverage
- Investigating discovered bugs
- Refactoring tests to be more maintainable
## Test Naming
**Pattern:** `[outcome] when [condition]`
### Good Names (Describe Outcomes)
```typescript
// β
GOOD: Describes what happens
it('returns empty array when input is null', () => {});
it('returns err NOT_FOUND when user does not exist', () => {});
it('calculates tax correctly for tax-exempt items', () => {});
it('preserves original order when duplicates removed', () => {});
it('returns ok with user when found in database', () => {});
```
### Bad Names (Describe Actions)
```typescript
// β BAD: Describes what you're doing, not what happens
it('test null input', () => {}); // What about null input?
it('should work', () => {}); // What does "work" mean?
it('handles edge cases', () => {}); // Which edge cases?
it('email validation test', () => {}); // What's being validated?
it('test getUser', () => {}); // What does getUser do?
```
### The Specification Test
Your test name should read like a specification. If someone reads ONLY the test names, they should understand the complete behavior of the system.
```typescript
describe('getUser', () => {
it('returns ok with user when found in database', () => {});
it('returns err NOT_FOUND when user does not exist', () => {});
it('returns err DB_ERROR when database connection fails', () => {});
it('returns err VALIDATION_ERROR when userId is empty string', () => {});
});
// Reading just these names tells you everything getUser does.
```
## Assertion Best Practices
### Assert Specific Values
```typescript
// β WEAK - passes even if completely wrong data
it('returns user when found', async () => {
const result = await getUser({ userId: '123' }, deps);
expect(result).toBeDefined();
expect(result.ok).toBeTruthy();
if (result.ok) {
expect(result.value).toBeTruthy();
}
});
// β
STRONG - catches actual bugs
it('returns ok with user when found in database', async () => {
const mockUser = { id: '123', name: 'Alice', email: '[email protected]' };
const deps = mock<GetUserDeps>();
deps.db.findUser.mockResolvedValue(mockUser);
const result = await getUser({ userId: '123' }, deps);
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.value).toEqual(mockUser);
expect(result.value.email).toBe('[email protected]');
}
});
```
### Match Assertions to Test Title
```typescript
// β TEST SAYS "different IDs" BUT ASSERTS COUNT
it('generates different IDs for each call', () => {
const id1 = generateId();
const id2 = generateId();
expect([id1, id2]).toHaveLength(2); // WRONG: doesn't check they're different!
});
// β
ACTUALLY VERIFIES DIFFERENT IDs
it('generates different IDs for each call', () => {
const id1 = generateId();
const id2 = generateId();
expect(id1).not.toBe(id2); // RIGHT: verifies the claim
expect(id1).toMatch(/^[a-z0-9-]+$/); // Also verify format
expect(id2).toMatch(/^[a-z0-9-]+$/);
});
```
### Testing Result Types
When testing functions that return `Result<T, E>`, assert on the specific error type and value:
```typescript
// β WEAK - doesn't verify error type
it('handles missing user', async () => {
const result = await getUser({ userId: 'missing' }, deps);
expect(result.ok).toBe(false);
});
// β
STRONG - verifies exact error
it('returns err NOT_FOUND when user does not exist', async () => {
const deps = mock<GetUserDeps>();
deps.db.findUser.mockResolvedValue(null);
const result = await getUser({ userId: 'missing' }, deps);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toBe('NOT_FOUND');
}
});
```
### Avoid Implementation Coupling
```typescript
// β BRITTLE - tests implementation details
it('queries database with correct SQL', async () => {
const deps = mock<GetUserDeps>();
await getUser({ userId: '123' }, deps);
expect(deps.db.findUser).toHaveBeenCalledWith('123');
// What if we change to findUser({ where: { id: '123' } })? Test breaks.
});
// β
FLEXIBLE - tests behavior
it('returns ok with user when found in database', async () => {
const mockUser = { id: '123', name: 'Alice' };
const deps = mock<GetUserDeps>();
deps.db.findUser.mockResolvedValue(mockUser);
const result = await getUser({ userId: '123' }, deps);
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.value).toEqual(mockUser);
}
// Implementation can change (SQL, ORM, etc.) and test still passes
});
```
## Test Structure
### Arrange-Act-Assert
```typescript
it('calculates total with tax for non-exempt items', () => {
// Arrange: Set up test data and mocks
const item = { price: 100, taxExempt: false };
const taxRate = 0.1;
const deps = mock<CalculateTotalDeps>();
deps.config.getTaxRate.mockReturnValue(taxRate);
// Act: Execute the behavior
const result = calculateTotal({ item }, deps);
// Assert: Verify the outcome
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.value).toBe(110);
}
});
```
### One Concept Per Test
```typescript
// β MULTIPLE CONCEPTS - hard to diagnose failures
it('validates and processes order', () => {
expect(validate(order)).toBe(true);
expect(process(order).ok).toBe(true);
if (process(order).ok) {
expect(sendEmail).toHaveBeenCalled();
}
});
// β
SINGLE CONCEPT - clear failures
it('accepts valid orders', () => {
const result = validate(validOrder);
expect(result).toBe(true);
});
it('rejects orders with negative quantities', () => {
const result = validate(negativeQuantityOrder);
expect(result).toBe(false);
});
it('sends confirmation email after processing', async () => {
const deps = mock<ProcessOrderDeps>();
deps.mailer.send.mockResolvedValue(undefined);
const result = await processOrder(validOrder, deps);
expect(result.ok).toBe(true);
expect(deps.mailer.send).toHaveBeenCalledWith(validOrder.customerEmail);
});
```
## Edge Case Checklists
When testing a function, systematically consider these edge cases based on input types.
### Numbers
- [ ] Zero
- [ ] Negative numbers
- [ ] Very large numbers (near MAX_SAFE_INTEGER)
- [ ] Very small numbers (near MIN_SAFE_INTEGER)
- [ ] Decimal precision (0.1 + 0.2)
- [ ] NaN
- [ ] Infinity / -Infinity
- [ ] Boundary values (off-by-one at limits)
```typescript
describe('calculateTotal', () => {
it('returns err INVALID when price is negative', () => {});
it('returns err INVALID when price is zero', () => {});
it('returns err INVALID when price is NaN', () => {});
it('handles very large prices near MAX_SAFE_INTEGER', () => {});
it('handles decimal precision correctly', () => {
// 0.1 + 0.2 = 0.30000000000000004
});
});
```
Related in Writing & Docs
jax-development
IncludedUse this skill when the user is writing, debugging, profiling, refactoring, reviewing, benchmarking, parallelising, exporting, or explaining JAX code, or when they mention JAX, jax.numpy, jit, grad, value_and_grad, vmap, scan, lax, random keys, pytrees, jax.Array, sharding, Mesh, PartitionSpec, NamedSharding, pmap, shard_map, Pallas, XLA, StableHLO, checkify, profiler, or the JAX repo. It helps turn NumPy or PyTorch-style code into pure functional JAX, fix tracer/control-flow/shape/PRNG bugs, remove recompiles and host-device syncs, choose transforms and sharding strategies, inspect jaxpr/lowering/IR, and benchmark compiled code correctly.
nature-article-writer
IncludedDrafts, rewrites, diagnostically critiques, and style-calibrates primary research manuscripts for Nature and Nature Portfolio journals. Use when the user wants a Nature-style title, summary paragraph or abstract, introduction, results, discussion, methods, figure legends, presubmission enquiry, cover letter, reviewer response, or when a scientific draft sounds generic, jargon-heavy, structurally weak, or AI-ish and needs precise, broad-reader-friendly prose without inventing data, analyses, or references. Best for primary research articles and letters rather than reviews or press releases unless explicitly adapting one.
deckrd
IncludedDocument-driven framework that derives requirements, specifications, implementation plans, and executable tasks from goals through structured AI dialogue. Use when user says "write requirements", "create spec", "plan implementation", "derive tasks", "structure this feature", "break down into tasks", or "document this module". Also use for reverse engineering existing code into docs (/deckrd rev). Do NOT use for direct code writing β use /deckrd-coder after tasks are generated. Do NOT use when the user only wants to run or fix existing code without planning.
clinical-decision-support
IncludedGenerate professional clinical decision support (CDS) documents for pharmaceutical and clinical research settings, including patient cohort analyses (biomarker-stratified with outcomes) and treatment recommendation reports (evidence-based guidelines with decision algorithms). Supports GRADE evidence grading, statistical analysis (hazard ratios, survival curves, waterfall plots), biomarker integration, and regulatory compliance. Outputs publication-ready LaTeX/PDF format optimized for drug development, clinical research, and evidence synthesis.
handling-sf-data
IncludedSalesforce data operations with 130-point scoring. Use this skill to create, update, delete, bulk import/export, generate test data, and clean up org records using sf CLI and anonymous Apex. TRIGGER when: user creates test data, performs bulk import/export, uses sf data CLI commands, needs data factory patterns for Apex tests, or needs to seed/clean records in a Salesforce org. DO NOT TRIGGER when: SOQL query writing only (use querying-soql), Apex test execution (use running-apex-tests), or metadata deployment (use deploying-metadata).
accelint-ac-to-playwright
IncludedConvert and validate acceptance criteria for Playwright test automation. Use when user asks to (1) review/evaluate/check if AC are ready for automation, (2) assess if AC can be converted as-is, (3) validate AC quality for Playwright, (4) turn AC into tests, (5) generate tests from acceptance criteria, (6) convert .md bullets or .feature Gherkin files to Playwright specs, (7) create test automation from requirements. Handles both bullet-style markdown and Gherkin syntax with JSON test plan generation and validation.