You're browsing the documentation for an old version of Livewire. Consider upgrading your project to Livewire 3.x.

Testing

Livewire offers a powerful set of tools for testing your components.

Here's a Livewire component and a corresponding test to demonstrate the basics.

            
1use Livewire\Component;
2 
3class CreatePost extends Component
4{
5 public $title;
6 
7 public function mount($initialTitle = '')
8 {
9 $this->title = $initialTitle;
10 }
11 
12 public function create()
13 {
14 auth()->user()->posts()->create($this->validate([
15 'title' => 'required',
16 ]);
17 
18 return redirect()->to('/posts');
19 }
20 
21 public function render()
22 {
23 return view('livewire.create-post');
24 }
25}
            
1<form wire:submit.prevent="create">
2 <input wire:model="title" type="text">
3 
4 <button>Create Post</button>
5</form>
        
1class CreatePostTest extends TestCase
2{
3 /** @test */
4 function can_create_post()
5 {
6 $this->actingAs(factory(User::class)->create());
7 
8 Livewire::test(CreatePost::class)
9 ->set('title', 'foo')
10 ->call('create');
11 
12 $this->assertTrue(Post::whereTitle('foo')->exists());
13 }
14 
15 /** @test */
16 function can_set_initial_title()
17 {
18 $this->actingAs(factory(User::class)->create());
19 
20 Livewire::test(CreatePost::class, ['initialTitle' => 'foo'])
21 ->assertSet('title', 'foo');
22 }
23 
24 /** @test */
25 function title_is_required()
26 {
27 $this->actingAs(factory(User::class)->create());
28 
29 Livewire::test(CreatePost::class)
30 ->set('title', '')
31 ->call('create')
32 ->assertHasErrors(['title' => 'required']);
33 }
34 
35 /** @test */
36 function is_redirected_to_posts_page_after_creation()
37 {
38 $this->actingAs(factory(User::class)->create());
39 
40 Livewire::test(CreatePost::class)
41 ->set('title', 'foo')
42 ->call('create')
43 ->assertRedirect('/posts');
44 }
45}

Testing Component Presence

Livewire registers a handy PHPUnit method to test for a component's presence on a page.

        
1class CreatePostTest extends TestCase
2{
3 /** @test */
4 function post_creation_page_contains_livewire_component()
5 {
6 $this->get('/posts/create')->assertSeeLivewire('create-post');
7 }
8}

All Available Test Methods

        
1Livewire::actingAs($user);
2// Set the provided user as the session's logged in user for the test
3 
4Livewire::test('foo', ['bar' => $bar]);
5// Test the "foo" component with "bar" set as a parameter.
6 
7->set('foo', 'bar');
8// Set the "foo" property (`public $foo`) to the value: "bar"
9 
10->call('foo');
11// Call the "foo" method
12 
13->call('foo', 'bar', 'baz');
14// Call the "foo" method, and pass the "bar" and "baz" parameters
15 
16->emit('foo');
17// Fire the "foo" event
18 
19->emit('foo', 'bar', 'baz');
20// Fire the "foo" event, and pass the "bar" and "baz" parameters
21 
22->assertSet('foo', 'bar');
23// Asserts that the "foo" property is set to the value "bar"
24 
25->assertNotSet('foo', 'bar');
26// Asserts that the "foo" property is NOT set to the value "bar"
27 
28->assertSee('foo');
29// Assert that the string "foo" exists in the currently rendered content of the component
30 
31->assertDontSee('foo');
32// Assert that the string "foo" DOES NOT exist in the currently rendered content of the component
33 
34->assertSeeHtml('<h1>foo</h1>');
35// Assert that the string "<h1>foo</h1>" exists in the currently rendered HTML of the component
36 
37->assertDontSeeHtml('<h1>foo</h1>');
38// Assert that the string "<h1>foo</h1>" DOES NOT exist in the currently rendered HTML of the component
39 
40->assertSeeInOrder(['foo', 'bar']);
41// Assert that the string "foo" exists before "bar" in the currently rendered content of the component
42 
43->assertSeeHtmlInOrder(['<h1>foo</h1>', '<h1>bar</h1>']);
44// Assert that the string "<h1>foo</h1>" exists before "<h1>bar</h1>" in the currently rendered content of the component
45 
46->assertEmitted('foo');
47// Assert that the "foo" event was emitted
48 
49->assertEmitted('foo', 'bar', 'baz');
50// Assert that the "foo" event was emitted with the "bar" and "baz" parameters
51 
52->assertNotEmitted('foo');
53// Assert that the "foo" event was NOT emitted
54 
55->assertHasErrors('foo');
56// Assert that the "foo" property has validation errors
57 
58->assertHasErrors(['foo', 'bar']);
59// Assert that the "foo" AND "bar" properties have validation errors
60 
61->assertHasErrors(['foo' => 'required']);
62// Assert that the "foo" property has a "required" validation rule error
63 
64->assertHasErrors(['foo' => ['required', 'min']]);
65// Assert that the "foo" property has a "required" AND "min" validation rule error
66 
67->assertHasNoErrors('foo');
68// Assert that the "foo" property has no validation errors
69 
70->assertHasNoErrors(['foo', 'bar']);
71// Assert that the "foo" AND "bar" properties have no validation errors
72 
73->assertNotFound();
74// Assert that an error within the component caused an error with the status code: 404
75 
76->assertRedirect('/some-path');
77// Assert that a redirect was triggered from the component
78 
79->assertUnauthorized();
80// Assert that an error within the component caused an error with the status code: 401
81 
82->assertForbidden();
83// Assert that an error within the component caused an error with the status code: 403
84 
85->assertStatus(500);
86// Assert that an error within the component caused an error with the status code: 500
87 
88->assertDispatchedBrowserEvent('event', $data);
89// Assert that a browser event was dispatched from the component using (->dispatchBrowserEvent(...))
← Previous Topic Inline Scripts
Next Topic → Security