Latest Posts

Efficiently testing components with Laravel Livewire's testing utilities


I've been writing some tests to check my new subscription form functions as expected. I show how easy it is to write tests to make assertions against a Livewire component.

As touched on recently, testing is important! I just wanted to quickly document my experience writing Pest tests for Livewire components. Testing can be tricky with abstractions but fortunately, Livewire is a well considered framework and has all the testing utilities needed.

I've modularised my subscription elements which you'll notice in the code snippet below, but that's not important, what is, is how we test against the Livewire component.


use Livewire\Livewire;
use Modules\Subscribe\Actions\CreateSubscriber;
use Modules\Subscribe\Livewire\Subscribe;
use Tests\TestCase;


it('can render the component', function () {
    /// When we render the component, check we are loading the right view
    /// and that we see an expected element from the form itself
        ->assertSee('I agree to receive occasional')

it('can validate the form', function () {
    /// When we submit the form with invalid values, we should see
    /// the appropriate errors
        ->set('name', '')
        ->set('email', '')
        ->set('agree', false)
        ->assertHasErrors(['name', 'email', 'agree']);

it('can submit the form', function () {
    $this->mock(CreateSubscriber::class, function ($mock) {

    /// When we submit the form with valid values, we should see
    /// a success message and no errors
        ->set('name', 'John Doe')
        ->set('email', '')
        ->set('agree', true)
        ->assertSee('Thanks for signing up!')

You'll notice on the highlighted lines, that I'm mocking my CreateSubscriber class, I've pulled this out using the Action pattern. This allows me to write tests for that element separately, without having to test the full functionality in this test. So, I'm just making sure that it's called, and any testing for that particular action will be performed in another file.

So the common pattern is wrapping your component in Livewire::test() and then chaining ->set() to set the components values and ->call() to call the methods against it. In the case of my subscribe form, the save function would normally be called when the form is submitted.

We can then check for the presence of errors, or lack of, and run partial checks on the output to make sure our messaging has changed. So as you can see it's so easy to write tests for Livewire components. Thank you Livewire!

If you haven't yet seen my subscription form in action, check it out here!