bobwurtz's avatar

Dusk Test for Livewire Pull Request

Hey All,

I use Livewire in my applications and there is currently a pull request that is waiting on tests to be written before it can be approved. I have done some simple browser tests before so I was trying to write a test for this PR to get things moving. I've run into some issues and I'm hoping someone can point me in the right direction. Also, I've never attempted to contribute to an open-source project before, so if I'm failing to follow any best practices or something seems off please let me know.

I started by cloning the Livewire repo and checking out the code from the PR. I'm unable to run Dusk tests in that folder because php artisan cannot be found. That makes sense since the repo itself is not a Laravel app, but then how am I supposed to write tests and execute them locally to test everything before pushing a commit with an update? Once that happened, I tried to make a test in one of my local applications. I copied the format of the tests from the Livewire source code

<?php
// CorruptDataTest.php

namespace Tests\Browser\LivewireTesting;

use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
use Livewire\Livewire;

class CorruptDataTest extends DuskTestCase
{
    /** @test */
    public function test()
    {
        $this->browse(function (Browser $browser) {
            Livewire::visit($browser, Component::class)
                ->assertSee('Add Number');

        });
    }
}
<?php
// Component.php

namespace Tests\Browser\LivewireTesting;

use Illuminate\Support\Facades\View;
use Livewire\Component as BaseComponent;

class Component extends BaseComponent
{
    public $string = 'â';
    public $number = 0;

    public function render()
    {
        $this->count++;

        return View::file(__DIR__.'/view.blade.php');
    }
}
<!-- view.blade.php -->
<x-landing-layout>

    <div>
        <div>{{ $string }}</div>
        <div>{{ $number }}</div>
        <button id="addButton" wire:click="addNumber">Add number</button>
    </div>


</x-landing-layout>

I always end up stuck on an error that says Call to undefined method [waitForLivewireToLoad]. I've tried copying the TestCase.php and I've tried installing (Livewire Dusk Testbench)[https://github.com/joshhanley/livewire-dusk-testbench]. I can run the test successfully without Livewire::visit but I'm assuming that will be needed for the test to be accepted into the Livewire code.

Any advice or guidance on this issue would be greatly appreciated. Thank you.

0 likes
2 replies
Braunson's avatar

If you take a look at the Livewire repo in an example test that uses that waitForLivewire method then take a look at the TestCase it extends, you'll see on line 43 of this file that the use Browser:mixing(new DuskBrowserMacros) which is where the macro/method waitForLivewireToLoad is defined.

If you are not already including that mixin, I would suggest doing so.

Please or to participate in this conversation.