Livewire v2.4.0 was just released with added support for a --test
option when running the php artisan livewire:make
command.
The new --test
option, when included, will scaffold a test class for the component in the tests/Features/Livewire
directory similar to the php artisan make:test
command.
php artisan livewire:make ExampleComponent --test
<?php
namespace Tests\Feature\Livewire;
use App\Http\Livewire\ExampleComponent;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Livewire\Livewire;
use Tests\TestCase;
class ExampleComponentTest extends TestCase
{
/** @test */
public function the_component_can_render()
{
$component = Livewire::test(ExampleComponent::class);
$component->assertStatus(200);
}
}
I've been working on several Livewire projects lately and creating new components every day, so I had the idea to create this option in order to save me (and other Livewire users) time. Instead of having to create a new component, then manually creating a test class, just add the --test
option and start writing your tests!
Filed in:
TALL stack (Tailwind CSS, Alpine.js, Laravel, and Livewire) consultant and owner of roasted.dev.