Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KeithHalsey's avatar

Testing with Dusk and Pest (and Sail)

Hi, I have a laravel application running in Sail. I can write feature tests in Pest and everything runs fine when I run sail pest. I have installed Dusk, and again, its all working fine when I run sail dusk. However for the dusk tests, I have to run it using phpunit syntax rather than pest syntax else it gives an error.

e.g. this is fine:

namespace Tests\Browser;

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

class exampleTest extends DuskTestCase
{
    public function testExample(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('/')
                    ->assertSee('Laravel');
        });
    }
}

But this throws an error when I run it:

use Laravel\Dusk\Browser;
 
it('test example', function() {
			$this->browse(function (Browser $browser) {
            	$browser->visit('/')
                    ->assertSee('Laravel');
                });
});

And it gives the error:

   Pest\Exceptions\InvalidPestCommand

  Please run [./vendor/bin/pest] instead.

Any ideas? Thanks

0 likes
2 replies
LaryAI's avatar
Level 58

It seems like you're trying to use Pest's syntax to write Dusk tests, which is not directly supported out of the box. Pest is a testing framework that is built on top of PHPUnit, and while it provides a more expressive syntax for writing tests, it doesn't automatically work with Laravel Dusk without some additional setup.

However, you can integrate Pest with Dusk by creating a helper function that allows you to use Pest's syntax for Dusk tests. Here's how you can do it:

First, create a helper function in a Pest test file or a dedicated helper file that you include in your Pest tests. This function will be responsible for creating a DuskTestCase instance and running the browser test:

// tests/PestDusk.php

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

function dusk(Closure $callback)
{
    $test = new class extends DuskTestCase {
        public function test(Closure $callback)
        {
            $this->browse($callback);
        }
    };

    test()->test($callback);
}

Now, you can use this dusk function in your Pest tests like so:

// tests/Browser/exampleTest.php

use Laravel\Dusk\Browser;

it('displays the correct welcome message', function () {
    dusk(function (Browser $browser) {
        $browser->visit('/')
                ->assertSee('Laravel');
    });
});

Make sure to include the helper file in your Pest tests if it's not in the same file:

// tests/Pest.php

require_once 'PestDusk.php';

Now, when you run sail pest, it should correctly execute your Dusk tests using Pest's syntax.

Keep in mind that this is a workaround to use Pest's syntax with Dusk, and it may not support all features of Pest or Dusk. If you encounter any issues, you may need to adjust the helper function or revert to using the standard PHPUnit syntax for Dusk tests.

KeithHalsey's avatar
KeithHalsey
OP
Best Answer
Level 2

Ah - I missed something stupid. I should have been running as sail dusk --pest and not just sail dusk, and then also I needed to add

uses(
    Tests\DuskTestCase::class,
)->in('Browser');

to my Pest.php file.

Please or to participate in this conversation.