In this episode, we dive into setting up a separate database for our Laravel Dusk browser tests. First, we take a look at how unit and feature tests typically use an in-memory SQLite database to make tests run super fast and keep test data from polluting our local development database. We see how simple it is to do this by tweaking the phpunit.xml
file and using the default in-memory settings.
But when it comes to Laravel Dusk—which actually opens up a real browser to simulate users interacting with the app—we hit a snag: Dusk can't use that speedy in-memory SQLite DB because browser-based requests are made outside of PHP's process. Instead, Dusk needs to read and write data to a physical database. We walk through why it's not a great idea to just use the same MySQL database you use for your day-to-day development: your tests would mix with your real data, creating a mess.
To solve this, we create a brand-new database just for Dusk tests (like myapp_testing
). We set the connection details in a dedicated Dusk environment file, and then update our test setup to run migrations before each test. That way, we always have a fresh database structure that gets cleaned up after the tests are done. By the end of the episode, Dusk is up and running with a totally isolated database, so you can confidently write your browser tests without risking your local data or worrying about leftovers. This sets a solid foundation to continue building even more robust Dusk tests in the next lessons!