spatie/pest-expectations
Add expressive, chainable expectations to Pest tests with Spatie’s helpers. Provides handy assertion-style methods for cleaner, more readable tests, letting you validate values, types, strings, arrays, and more with minimal boilerplate.
Install via Composer:
composer require --dev spatie/pest-expectations
No configuration needed—just start using expectations in your Pest tests. The package auto-registers itself and extends Pest’s expect() function. The first go-to use case is replacing verbose assertEquals, assertTrue, etc., with expressive alternatives like expect($value)->toBeInt() or expect($collection)->toContain($item). Check the README for the full list of expectations and examples.
expect($user)->toBeActive(), expect($response)->toHaveStatus(201), expect($date)->toBeToday().expect($user)
->toBeActive()
->toHaveEmail('jane@example.com')
->toHaveNameStartWith('Jane');
beforeEach, afterEach, or test closures—no state management required.assertEquals($expected, $actual) with expect($actual)->toBe($expected) and assertArrayHasKey($key, $array) with expect($array)->toHaveKey($key).expect($this->get('/'))->toBeSuccessful() in feature tests.toBeLowercase() and toBeUppercase() are strict (e.g., toBeLowercase() rejects mixed-case strings). Use toLower() + toBe('...') for flexible checks if needed.toBe(0.1 + 0.2) fails due to IEEE 754—use toBeApproximately(0.3, 0.0001) instead.Expectation::macro('toBeActive', fn () => $this->toHaveProperty('status', 'active')).->withMessage('User should be active').expect() macros: If you define your own expect() macros, ensure they’re registered after this package’s service provider loads (it auto-registers, so manual registration is unnecessary).How can I help you explore Laravel packages today?