Laravel Dusk was released with Laravel 5.4 and gave developers an incredibly easy API to create and run automated browser tests. However, I came across an issue while running browser tests with SQLite.
I am not going to explain how to get started with Laravel Dusk. The documentation already does an excellent job and I would just be repeating what is already there.
The Problem
When I am developing Laravel locally, I like to use SQLite since it is easy to use and doesn’t require a database server to be run on the development machine. However, if you want to use SQLite with Dusk, it will use your development database by default. This was causing my database to be altered since I have the DatabaseMigrations trait within my tests.
The DatabaseMigrations trait will run your migrations with each test. Hence, my development database was being altered.
Why was this happening? It came down to how I had my .env
file setup. Dusk was using my local .env
settings which was pointing at my SQLite database.
After some digging around, I discovered Dusk will use it’s own .env
file if it exists. The naming convention follows .env.dusk.{environment}
, which in my case is .env.dusk.local
.
But this doesn’t exactly fix the problem. Within the .env.dusk.local file, a new database configuration will need to be used to point the Dusk session at the other SQLite database.
The solution
Start by creating a new SQLite database within the database
directory. I named mine dusk.sqlite
.
Next, create a new database configuration within the config/database.php
file.
<?php
// config/database.php
'dusk' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('dusk.sqlite')),
'prefix' => '',
],
If you haven’t created your dusk .env.dusk.local
file, do so now and set the DB_CONNECTION
to “dusk”. I just copied my .env
file over and renamed it.
DB_CONNECTION=dusk
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=
# DB_USERNAME=homestead
# DB_PASSWORD=secret
It may also help to start the server with the dusk .env
file too.
$ php artisan serve --env=dusk.local
Now start Dusk and watch it go.