Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Quick Tip: Customizing Default Stubs

Premium
1 min read

Writing automated tests for your application is crucial. Let's see how we can do it for a Livewire component: the syntax is very similar to typical PHPUnit tests.


Creating a Test

Creating tests for Livewire is the same as for the Laravel application. We can create a test file using the artisan command.

php artisan make:test HelperTextTest

This command will create a HelperTextTest.php test file in the tests/Feature directory.

From here on, we can write tests for Livewire components.

Alternatively, a test can be generated when creating a Livewire component.

php artisan make:livewire create-post --test

After appending the --test flag in addition to creating a Livewire component the tests/Feature/Livewire/CreatePostTest.php test file will be created.


First Test: Check The Text is Seen

For the first test, let's write a test for the component we made in the previous lesson to show helper text.

tests/feature/HelperTextTest.php:

use Livewire\Livewire;
use App\Livewire\ShowHelp;
 
class HelperTextTest extends TestCase
{
public function test_can_see_helper_text()
{
Livewire::test(ShowHelp::class)
->assertDontSee('Lorem Ipsum')
->toggle('showHelp')
->assertSee('Lorem Ipsum');
}
}

In this test, first, we tell Livewire which component we test. Then comes the first assertion that we don't see the helper text.

Next, we toggle the showHelp property, which is being set to true, and finally assert that we see the helper text.


Second Test: Check Property Set and DB Updated

For the second test, we will test a...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

S
Smash ✓ Link copied!

php artisan livewire:stubs now create seven files, livewire.attribute.stub, livewire.form.stub, livewire.pest.stub were added.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.