Use Laravel Dusk to Test JS-Based Applications

Developer Tools

March 22nd, 2022

laravel-husk.png

Laravel Husk is a thin, light scaffolded Laravel Dusk environment. It allows you to test JavaScript applications with PHP using Pest without scaffolding an entire Laravel application.

Once you've followed the installation instructions, you can write Dusk feature tests like any other application using Dusk. You'll have a browser folder, and inside of that folder, you can run your Dusk test suite with the following command:

1# Start your JS application
2npm run dev
3 
4# Run the Dusk test suite
5php application pest:dusk

Here's an example of a test from a demo Nuxt application using Husk for end-to-end feature tests:

1use Laravel\Dusk\Browser;
2use Tests\Pages\HomePage;
3 
4it('loads', function () {
5 $this->browse(function (Browser $browser) {
6 $browser->visit(new HomePage)->assertSee('Welcome to your Nuxt Application');
7 });
8});

Learn More

There's not much more to learn beyond the Dusk and Pest documentation. Husk takes the pain out of setting up a Dusk environment outside of Laravel applications and provides an easy path to test JS applications with familiar tools.

The author has some example projects written in Nuxt, Gatsby, and Gridsome. These examples are helpful to visualize all the moving pieces involved in testing JS apps with Dusk. It looks like NextJS and Svelte are planned, but the links were broken when I looked through the Husk readme.

To get started in your own project, check out Laravel Husk on GitHub. The README also includes a GitHub actions template you can use to start running Dusk tests in your JS projects.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners