Menu

Faster Laravel tests: restore a snapshot instead of running migrate:fresh
📰

Faster Laravel tests: restore a snapshot instead of running migrate:fresh

DEV Community: laravel·Amjad Iqbal·9 days ago
#Nhb8ikJx
#devtolaravel#devto#dev#every#test
Long article (262 words) — no AI summary yet.
Reading 0:00
15s threshold

Laravel's RefreshDatabase runs migrate:fresh once in every test process: every single-test run, every CI job and every --parallel worker. Refresher is an open-source drop-in replacement that does it once and reuses the result.

How it works

The first test process migrates (and seeds) the test database and saves it as a snapshot.

Every process after that restores the snapshot, until a migration, seeder or factory changes.

SQLite snapshots are a file copy; MySQL and MariaDB use mysqldump and the mysql client.

Setup

composer require amjadiqbal / refresher -- dev

// tests/Pest.php pest ( ) -> extend ( Tests \ TestCase :: class ) -> use ( AmjadIqbal \ Refresher \ Testing \ RefreshesWithRefresher :: class ) -> in ( 'Feature' ) ;

Enter fullscreen mode

Exit fullscreen mode

For PHPUnit, use the same trait in your test class. LazilyRefreshesWithRefresher replaces LazilyRefreshDatabase .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.