Laravel, Docker, Pest and Browser Tests

Posted: 2025-12-16 11:44:20 by Alasdair Keyes

Direct Link | RSS feed


In recent years the PHP testing tool Pest has gained much traction. So much so, the Laravel testing documentation shows test suite examples in Pest as well as PHPUnit.

On the whole I'm happy with PHPUnit, however when performing browser-based tests with Laravel I have had no end of problems with getting Laravel's Dusk to work within Docker.

Pest has built-in browser support so I was interested in seeing how well it worked and how easily the browser tests were to set up.

Pest is a superset of PHPUnit so if you want to use Pest, you can still keep some of your existing PHPUnit tests and just use Pest where you want. Alternatively Pest does provide a tool to migrate your PHPUnit tests to Pest.

For the examples below, I'm assuming that you are running your Laravel/Vite/JS development environment in a single container or Virtual Machine.

Setting up Pest

The following is a migration from PHPUnit to Pest.

# Remove PHPUnit
composer remove phpunit/phpunit --dev

# Install Pest basic suite
composer require pestphp/pest --dev --with-all-dependencies
./vendor/bin/pest --init

Once this is done you can run either pest or Laravel's test tool

vendor/bin/pest
# or
./artisan test

Install Pest Browser Testing

composer require pestphp/pest-plugin-browser:^4.0 --dev
npm install playwright@latest
npx playwright install

Install Pest Migration plugin

As mentioned, Pest will continue to run your PHPUnit tests without issue. But if you wish to switch to Pest fully, you can install the Drift plugin

composer require pestphp/pest-plugin-drift --dev

Then convert your tests....

$ ./vendor/bin/pest --drift

  ✔✔✔✔✔✔✔✔✔✔✔.

   INFO  The [tests] directory has been migrated to PEST with 11 files changed.

After you have committed your changes, you can remove the drift plugin

composer remove pestphp/pest-plugin-drift --dev

A quick browser test

Now that we have Pest configured, it's worth checking it's working. Here's a quick test on the Laravel welcome page that comes with a fresh install.

<?php

test('Site gives 200 response', function () {
    $response = $this->get('/');

    $response->assertStatus(200);
});

test('Site produces correct output', function () {
    $page = visit('/')
    ->on()->desktop()
    ->inDarkMode();

    $page->assertSee('Laravel');
});

and run it...

$ vendor/bin/pest tests/Feature/PestExampleTest.php

   PASS  Tests\Feature\PestExampleTest
  ✓ Site gives 200 response                  0.13s  
  ✓ Site produces correct output             1.86s  

  Tests:    2 passed (2 assertions)
  Duration: 3.00s

Dockerfile

In development I run Laravel in the php:fpm-x.x Docker image with npm installed through apt for the Javascript/Vite functionality. The following snippet is the changes to make to Dockerfile.

# Existing docker config...

# Begin requirements for Pest/Playwright
RUN apt install -y libxdamage1 libgtk-3-0t64 libpangocairo-1.0-0 \
    libpango-1.0-0 libatk1.0-0t64 libcairo-gobject2 libcairo2 \
    libasound2t64 libgstreamer1.0-0 libgstreamer1.0-0 libgstreamer1.0-dev \
    libgtk-4-1 libgraphene-1.0-0 libxslt1.1 libwoff1 libvpx9 \
    libevent-2.1-7t64 libopus0 libgcrypt20 libgpg-error0 \
    libgstreamer-plugins-base1.0-0 libgstreamer1.0-0 libgstreamer-gl1.0-0 \
    libgstreamer-plugins-bad1.0-0 libgstreamer-plugins-base1.0-0 libflite1 \
    libwebpdemux2 libjxl0.11 libavif16 libharfbuzz-icu0 libwebpmux3 \
    libenchant-2-2 libsecret-1-0 libhyphen0 libmanette-0.2-0 libx264-164 \
    libx264-dev libnspr4 libnss3
RUN docker-php-ext-install sockets
# End requirements for Pest/Playwright

RUN composer install

RUN npm install

# Begin install Playwright for Pest browser testing
RUN npx playwright install
# End install Playwright for Pest browser testing

# Existing docker config...


If you found this useful, please feel free to donate via bitcoin to 1NT2ErDzLDBPB8CDLk6j1qUdT6FmxkMmNz

© Alasdair Keyes

IT Consultancy Services

I'm now available for IT consultancy and software development services - Cloudee LTD.



Happy user of Digital Ocean (Affiliate link)


Version:master-b651fea0f8


Validate HTML 5