You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Have you ever come across an issue where the traditionalRefreshDatabase trait takes ages to run tests when you have lots of migrations? If so, you may be after this package!
The Problem
Traditionally, theRefreshDatabase trait will runphp artisan migrate:fresh every time you run tests. After the first test, it will use transactions to roll back the data and run the next one, so subsequent tests are fast, but the initial test is slow. This can be really annoying if you are used to running a single test, as it could take seconds to run a single test.
The Solution
You don't need to runphp artisan migrate:fresh every time you run tests, only when you add a new migration or change an old one. TheFastRefreshDatabase trait will create a checksum of yourmigrations folder as well as your current Git branch. It will then create a checksum file in your application'sstorage/app directory. When your migrations change or your branch changes, the checksum won't match the cached one andphp artisan migrate:fresh is run.
When you don't make any changes, it will continue to use the same database without refreshing, which can speed up the test time by 100x!
Next, just replace the existingRefreshDatabase trait you are using in your TestCase file with theFastRefreshDatabase trait
<?phpnamespace Tests;use Illuminate\Foundation\Testing\TestCase as BaseTestCase;-use Illuminate\Foundation\Testing\RefreshDatabase;+use Plannr\Laravel\FastRefreshDatabase\Traits\FastRefreshDatabase;abstract class TestCase extends BaseTestCase{ use CreatesApplication;- use RefreshDatabase;+ use FastRefreshDatabase;}
The trait is unaware of what database or environment your tests are running within. Sometimes when running a parallel test after running individual tests, the migration checksum file may not have been deleted. You may have to manually delete the checksum file before running parallel tests.
About
Refresh your test databases faster than you've ever seen before 🚀