Livewire Make Command can now Scaffold a Component Test

News

February 24th, 2021

laravel-livewire.png

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

1<?php
2 
3namespace Tests\Feature\Livewire;
4 
5use App\Http\Livewire\ExampleComponent;
6use Illuminate\Foundation\Testing\RefreshDatabase;
7use Illuminate\Foundation\Testing\WithFaker;
8use Livewire\Livewire;
9use Tests\TestCase;
10 
11class ExampleComponentTest extends TestCase
12{
13 /** @test */
14 public function the_component_can_render()
15 {
16 $component = Livewire::test(ExampleComponent::class);
17 
18 $component->assertStatus(200);
19 }
20}

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:

Jason Beggs

TALL stack (Tailwind CSS, Alpine.js, Laravel, and Livewire) consultant and owner of roasted.dev.