Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Pest Plugin Livewire Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • State & Mutation Tests: Mount components with props (livewire(Counter::class, ['initial' => 5])), mutate state via ->set('count', 10) or ->call('increment'), and assert with ->assertSet('count', 10).
  • Validation & Errors: Submit invalid data via ->set('email', 'invalid') then ->call('submit') and assert ->assertHasErrors(['email' => 'required']).
  • Event Propagation: Test parent-child communication by emitting events: ->emit('update', $data) and assert ->assertEmitted('updated') or ->assertDispatched('public::updated').
  • Auth & DB Integration: Combine with Pest’s auth helpers:
    livewire(CreatePost::class)
        ->actingAs($user)
        ->set('title', 'Test')
        ->call('save')
        ->assertDatabaseCount('posts', 1);
    
  • Mock Dependencies: Inject mocks early using ->with() or global mocking:
    $this->mock(ApiClient::class)->shouldReceive('call')->once()->andReturn([]);
    livewire(LiveDashboard::class)->assertSee('Ready');
    

Gotchas and Tips

  • Strict Version Locking: This plugin requires Pest ≥4.3.1 and Livewire v3.7.4+ / v4. Use explicit version constraints in composer.json ("pestphp/pest": "^4.3", "livewire/livewire": "^3.7.4|^4"), or assertions like assertSet() silently fail.
  • Assertion Timing: ->assertDispatched() must be called immediately after ->emit()/->dispatch()—delayed assertions cause false positives due to Livewire’s batching.
  • Silent Failures in 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
    
  • Debugging Mid-Chain: Use ->dump() or ->dd() inside chains to inspect component()->model, but always remove them before committing—Pest’s dump helpers don’t auto-strip.
  • Avoid HTTP Assertions: ->assertOk() checks HTTP status only. Use ->assertSee()/->assertDontSee() for component output validation instead.
  • Async Handlers: For dispatch('event')->withDelay(), test emission and side effects separately—don’t rely on direct assertion chaining due to Laravel’s event queue timing.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4