Testing Laravel Applications with CircleCI & Dusk

I recently started a new project and wanted to give Laravel’s new test framework Dusk a try. Locally, this was quick to do and it worked first time – however when I then connected up CircleCI I needed to do a little extra work to get up and running. In short, the version of Google Chrome needs updating to a newer version and then things should be running well.

Firstly, make sure you’re up and running locally and your tests are passing (that’s half the work done, right?). You should have output similar to:

scott@inex.local / sample-app / master ✔ : php artisan dusk
PHPUnit 5.7.19 by Sebastian Bergmann and contributors.

… 3 / 3 (100%)

Time: 15.18 seconds, Memory: 16.00MB

OK (3 tests, 3 assertions)

So, we’re all good at this point. Next, get your circle.yml file looking something like the following:

machine:
  pre:
    - sudo apt-get update; USE_PRECOMPILE=true sudo -E circleci-install php 7.1.0
  timezone:
    Europe/London
  php:
    version: 7.1.0
  environment:
    APP_ENV: "testing"
  hosts:
      testing: 127.0.0.1

general:
artifacts:
- “tests/Browser/screenshots”
- “tests/Browser/console”

dependencies:
pre:
- rm /opt/circleci/php/$(phpenv global)/etc/conf.d/xdebug.ini

override:
- composer install
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
- sudo sh -c ‘echo “deb http://dl.google.com/linux/chrome/deb/ stable main” >> /etc/apt/sources.list.d/google-chrome.list’
- sudo apt-get update
- sudo apt-get install google-chrome-stable

test:
pre:
- “./vendor/laravel/dusk/bin/chromedriver-linux”:
background: true
- cp .env.testing .env
- cp .env.testing .env.dusk
- “php artisan serve”:
background: true

override:
- mkdir -p $CIRCLE_TEST_REPORTS/phpunit
- php artisan dusk --log-junit $CIRCLE_TEST_REPORTS/phpunit/junit.xml tests

This will make sure you’re using PHP 7.1, start the site running in the background and allow dusk to run. You’ll need to make sure that the APP_URL value in your .env.testing file is pointing to 127.0.0.1. In the example above, we’re making sure that CircleCI adds an entry to /etc/hosts for ‘testing’.

Providing your tests now work, you’ll have a big green bar in CircleCI.