pestphp/pest-plugin-livewire
Pest Plugin Livewire adds Livewire-specific testing helpers to Pest, making it easy to write expressive, fluent tests for Laravel Livewire components. Integrates seamlessly with Pest to simplify component assertions and interactions.
Install the plugin via Composer: composer require pestphp/pest-plugin-livewire --dev. If Pest isn’t set up yet, run pest --init. The plugin auto-registers, but verify pest.php includes pest()->use(\Pest\Livewire\Concerns\MakesLivewireCalls::class);. Your first test should validate component rendering and basic interaction:
it('renders a counter component', function () {
livewire(Counter::class)
->assertSee('0')
->call('increment')
->assertSee('1');
});
Start with simple render-then-interact flows using ->call(), ->assertSee(), and ->assertDontSee(). Check the plugin’s README for the full assertion set and explore Pest’s built-in helpers like ->actingAs() and ->with() for dependency injection.
livewire(Counter::class, ['initial' => 5])), mutate state via ->set('count', 10) or ->call('increment'), and assert with ->assertSet('count', 10).->set('email', 'invalid') then ->call('submit') and assert ->assertHasErrors(['email' => 'required']).->emit('update', $data) and assert ->assertEmitted('updated') or ->assertDispatched('public::updated').livewire(CreatePost::class)
->actingAs($user)
->set('title', 'Test')
->call('save')
->assertDatabaseCount('posts', 1);
->with() or global mocking:
$this->mock(ApiClient::class)->shouldReceive('call')->once()->andReturn([]);
livewire(LiveDashboard::class)->assertSee('Ready');
composer.json ("pestphp/pest": "^4.3", "livewire/livewire": "^3.7.4|^4"), or assertions like assertSet() silently fail.->assertDispatched() must be called immediately after ->emit()/->dispatch()—delayed assertions cause false positives due to Livewire’s batching.mount(): If a test hangs or fails unexpectedly, check if mount() hits the DB/auth. Mock those before calling livewire(), e.g.,
Auth::shouldReceive('user')->andReturn($user);
livewire(Profile::class); // now `mount()` won’t error
->dump() or ->dd() inside chains to inspect component()->model, but always remove them before committing—Pest’s dump helpers don’t auto-strip.->assertOk() checks HTTP status only. Use ->assertSee()/->assertDontSee() for component output validation instead.dispatch('event')->withDelay(), test emission and side effects separately—don’t rely on direct assertion chaining due to Laravel’s event queue timing.How can I help you explore Laravel packages today?