Before writing tests, ensure your Laravel application has the necessary testing environment configured:
Database Setup: Use an in-memory SQLite database for testing to keep the tests isolated and fast. Update your phpunit.xml
file:
Install Dependencies: Ensure you have PHPUnit and Laravel’s testing tools installed. Typically, these come pre-installed with Laravel.
Create a Test Class: Use Artisan to generate a test class for your Livewire component:
php artisan make:test LivewireComponentTest --unit
Let’s test a simple Livewire component. For this example, we’ll use a CounterComponent.
count++;
}
public function decrement()
{
$this->count--;
}
public function render()
{
return view('livewire.counter-component');
}
}
Count: {{ $count }}
Open the LivewireComponentTest.php
file and write the following test:
assertSee('Count: 0')
->call('increment')
->assertSee('Count: 1')
->call('decrement')
->assertSee('Count: 0');
}
}
Livewire::test()
: Creates a test instance of the Livewire component.
assertSee()
: Ensures the specified text is visible in the rendered output.
call()
: Simulates calling a method in the component.
Run the test using PHPUnit:
Livewire tests allow you to simulate user interactions such as clicking buttons, submitting forms, and updating properties.
For a form component that collects a user’s name:
validate([
'name' => 'required|min:3',
]);
}
public function render()
{
return view('livewire.form-component');
}
}
@error('name') {{ $message }} @enderror
/** @test */public function it_validates_the_name_field()
{
Livewire::test(FormComponent::class)
->set('name', '')
->call('submit')
->assertHasErrors(['name' => 'required']);
Livewire::test(FormComponent::class)
->set('name', 'Jo')
->call('submit')
->assertHasErrors(['name' => 'min']);
Livewire::test(FormComponent::class)
->set('name', 'John Doe')
->call('submit')
->assertHasNoErrors();
}
set()
: Sets a property value on the component.
assertHasErrors()
: Asserts that the component has validation errors for a specific field.
assertHasNoErrors()
: Asserts that there are no validation errors.
Livewire components often dispatch browser events. You can test these events to ensure they are triggered as expected.
For a notification component:
dispatch('notification', ['message' => 'Hello World']);
}
public function render()
{
return view('livewire.notification-component');
}
}
/** @test */public function it_dispatches_a_notification_event()
{
Livewire::test(NotificationComponent::class)
->call('triggerNotification')
->assertDispatched('notification', ['message' => 'Hello World']);
}
assertDispatched()
: Ensures that the specified browser event was dispatched with the correct payload.
With these tests, you can ensure:
Your Livewire components are functional and reliable.
User interactions and form submissions work as expected.
Browser events are triggered correctly.
Setting up a testing environment for Livewire components.
Writing basic tests for methods and properties.
Simulating user interactions and validating form submissions.
Testing browser events in Livewire components.
410 C, Jaina Tower-1, District Center, Janak Puri New Delhi-110058, India.
© Copyright 2025 Wontonee. All Right Reserved.