Pest testing

From event4u-app/agent-config · View interactive version

Pest testing is an AI agent skill from the GitHub repository event4u-app/agent-config. Use when writing, generating, or improving Pest tests for Laravel — clear intent, good coverage, maintainable structure, and alignment with project testing conventions.

How to use this skill

Skill file

pest-testing

When to use

Use this skill for all Laravel testing tasks, especially when working with:

This skill extends php-coder, laravel, and eloquent.

For prevention layers that fire before writing a test — TDD

discipline, mock-isolation gates, and the 12 process rationalizations

("I'll add the test after", "patch first, test later") — see

CODE0,

CODE0, and

CODE0.

Procedure: Write Pest tests

  1. Read the base skills first — apply php-coder, laravel, and eloquent where relevant.
  2. Check the project's test framework — confirm Pest is used and inspect existing tests.
  3. Match the current test style — naming, helpers, datasets, expectations, setup, traits, and folder structure.
  4. Check available factories and seeders — reuse existing test data patterns.
  5. Understand the behavior under test — inspect controllers, services, requests, policies, jobs, and models before writing tests.
  6. Prefer existing helpers — authentication helpers, custom assertions, base test classes, and shared setup.

Core testing principles

TDD workflow (Red-Green-Refactor)

For bug fixes and new features, prefer test-driven development:

  1. RED — Write a failing test that describes the expected behavior.
  2. Verify RED — Run the test, confirm it fails for the expected reason (missing feature,

not a typo or syntax error). If the test passes immediately, it tests existing behavior — fix it.

  1. GREEN — Write the minimal code to make the test pass. No extras, no "while I'm here".
  2. Verify GREEN — Run all tests, confirm the new test passes and nothing else broke.
  3. REFACTOR — Clean up code while keeping tests green.

Why test-first matters

Tests written after implementation pass immediately. Passing immediately proves nothing:

Bug fix TDD

For every bug fix: write a failing test that reproduces the bug FIRST, then fix it.

The test proves the fix works AND prevents regression.

TDD rationalization prevention

| Excuse | Reality |

|---|---|

| "Too simple to test" | Simple code breaks. Test takes 30 seconds. |

| "I'll test after" | Tests passing immediately prove nothing. |

| "Manual test is faster" | Manual doesn't prevent regression. You'll re-test every change. |

| "Test is hard to write" | Hard to test = hard to use. Simplify the design. |

| "Need to explore first" | Fine — throw away exploration code, start fresh with TDD. |

| "Existing code has no tests" | You're improving it. Add tests for what you touch. |

Laravel testing rules

Pest style rules

Pest-specific PHP rules

Do NOT add use statements for global classes like DateTimeImmutable, Exception,

stdClass, etc. — PHP will warn: "The use statement with non-compound name has no effect".

Avoiding flaky tests

a clear gap between "before" and "after" timestamps. Never rely on now() being different

between two lines of code — on fast hardware, they can be identical.

doesn't set them — previous tests in parallel may have modified the same record.

global state, specific row counts, or auto-increment IDs.

HTTP and API tests

Validation tests

Authorization tests

Database assertions

Snapshot testing with `coduo/php-matcher`

This project uses CODE0 for flexible snapshot assertions.

Pattern files live in snapshots/ directories next to the test files.

Pattern variables

Use pattern variables instead of hardcoded values in snapshot files.

This makes snapshots resilient to data changes while still enforcing type correctness.

| Pattern | Matches | Example |

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

| @boolean@ | true or false | 'is_active' => '@boolean@' |

| @integer@ | Any integer | 'id' => '@integer@' |

| @string@ | Any string | 'name' => '@string@' |

| @null@ | null | 'deleted_at' => '@null@' |

| @datetime@ | ISO datetime string | 'created_at' => '@datetime@' |

| @uuid@ | UUID string | 'uuid' => '@uuid@' |

| @array@ | Any array | 'items' => '@array@' |

| @double@ | Any float | 'amount' => '@double@' |

| @wildcard@ | Anything | 'data' => '@wildcard@' |

Combine with || for nullable fields: 'deleted_at' => '@null@||@datetime@'

Rules

Example snapshot file

// snapshots/user-resource.php
return [
    'id' => $id ?? '@integer@',
    'name' => $name ?? '@string@',
    'email' => $email ?? '@string@',
    'is_active' => $is_active ?? '@boolean@',
    'created_at' => $created_at ?? '@datetime@',
    'deleted_at' => $deleted_at ?? '@null@||@datetime@',
];

Example test usage

expect($response->json())
    ->toMatchPhpPatternFile(
        patternFile: __DIR__ . '/snapshots/user-resource.php',
        replacements: ['id' => $user->getId()],
    );

Fakes, mocks, and external boundaries

Factories and fixtures

Test quality analysis

When reviewing or auditing existing tests, check for these anti-patterns:

Test smells to detect

| Smell | Description | Fix |

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

| Overmocking | Too many mocks disconnect the test from reality | Replace mocks with real implementations or fakes |

| Fragile tests | Tests break with unrelated changes (e.g., asserting exact JSON structure) | Assert only meaningful fields |

| Flaky tests | Non-deterministic results (time, ordering, parallel state) | Use time travel, explicit ordering, isolated data |

| Giant tests | One test covers 5+ behaviors | Split into focused tests |

| Missing assertions | Test runs code but doesn't verify outcomes | Add meaningful assertions |

| Test duplication | Same scenario tested in multiple places | Consolidate or use datasets |

| Assertion roulette | Many assertions without clear failure messages | Use named assertions or split tests |

| Eager test | Tests too many things, making failures hard to diagnose | One behavior per test |

FIRST principles

Mock usage guidelines

What NOT to do

Output expectations

When generating Pest tests:

Filter Pest output

When triaging a verbose run, narrow the output with --filter (Pest)

plus targeted grep/rg instead of re-running the whole suite or

echoing all logs:

# Only run failing tests in one file
vendor/bin/pest tests/Feature/InvoiceTest.php --filter='creates invoice'

# Scan the test log for failures only
rg --color=never '^FAIL|Tests:' storage/logs/pest.log

# Inspect JSON output from data-driven tests
vendor/bin/pest --log-junit=pest.xml && rg '<failure' pest.xml

Output format

  1. Pest test file with descriptive test names and clear assertions
  2. Tests organized by happy path, validation, edge cases

Gotcha

Do NOT

What to test (generation checklist)

When generating new tests, focus on:

What NOT to test:

Quality over quantity — 5 meaningful tests beat 20 trivial ones.

Auto-trigger keywords

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.