speeding-up-laravel-tests

Diagnoses and reduces Laravel and Pest test suite runtime through environment, faking, and database optimizations.

999|201|Updated May 6, 2014
One-click install
npx skills add https://github.com/freekmurze/dotfiles --skill speeding-up-laravel-tests
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: speeding-up-laravel-tests
Source: https://github.com/freekmurze/dotfiles/tree/main/config/claude/skills/speeding-up-laravel-tests
Command: npx skills add https://github.com/freekmurze/dotfiles --skill speeding-up-laravel-tests

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Laravel and Pest test suites often slow down as projects grow, making CI pipelines expensive and local feedback loops painful. This Skill provides a prioritized checklist of proven techniques to find and eliminate the biggest sources of test slowness.

Core Features & Use Cases

  • Environment Fixes: Set BCRYPT_ROUNDS=4, disable XDebug and pcov, disable background packages like Pulse and Telescope, and cache config and routes with WithCachedConfig and WithCachedRoutes.
  • Faking External Interactions: Apply Notification, Mail, Bus, Event, and Queue fakes, block stray HTTP requests with Http::preventingStrayRequests(), and use Sleep::fake() and Exceptions::fake() to stop real waits and error reporting.
  • Database and Factory Optimization: Switch to LazilyRefreshDatabase, audit nested factory create() calls, and reuse parent models with $factory->recycle().
  • Use Case: Your CI pipeline takes 20 minutes and individual auth tests take seconds each. Apply the checklist to set BCRYPT_ROUNDS=4, disable XDebug, and run pest --profile to find the slowest tests.

Quick Start

Ask the AI to analyze why my Laravel Pest test suite is slow and apply the recommended optimizations.

Frequently Asked Questions about speeding-up-laravel-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I speed up slow Laravel tests?

Start with environment fixes: set BCRYPT_ROUNDS=4 in .env.testing, disable XDebug and pcov, and disable background packages like Pulse and Telescope. Then fake external services, switch to LazilyRefreshDatabase, and run pest --profile to find the slowest tests.

How to find the slowest tests in a Pest test suite?

Run ./vendor/bin/pest --profile to print the slowest tests per shard. Patterns in the top-10 slowest tests often apply suite-wide, so fixing those patterns gives the biggest overall win.

What is the difference between RefreshDatabase and LazilyRefreshDatabase?

LazilyRefreshDatabase only runs migrations and truncation when a test actually touches the database, while RefreshDatabase pays that cost on every test. Tests that never hit the DB skip the overhead entirely with the lazy variant.

Why are my Laravel auth tests so slow?

Password hashing dominates auth tests because the default BCRYPT_ROUNDS is 12. Set BCRYPT_ROUNDS=4 in .env.testing or phpunit.xml to make hashing cheap during tests without affecting production.

Does Http::preventingStrayRequests catch all outgoing HTTP calls?

No, it only catches requests made through Laravel's HTTP client. Direct Guzzle or cURL usage bypasses it, so you need to audit those separately when hunting for slow stray requests in tests.

Why does Sleep::fake not speed up my retry tests?

Sleep::fake only works when your code uses Laravel's Sleep helper rather than PHP's native sleep() function. Use Sleep::fake(syncWithCarbon: true) and refactor production code to call the Sleep helper for retries and backoffs.