Pest's Spicy Summer Release
"Spicy Summer" is the codename assigned to Pest 2.9.
On March 20, 2023, we proudly introduced Pest 2.0, marking it as our most significant release to date, with more than 7 million downloads at the time of writing. This version showcased a remarkable architectural plugin, an 80% speed improvement in parallel testing, profiling options, and numerous other features.
As we approach summer, we are thrilled to announce our upcoming release: the highly anticipated "Spicy Summer" release. This release brings an array of exciting features that will make it feel like a major version without actually being one - it's Pest v2.9.0 - so it's just a "composer update" away from you. Without further delay, let's dive into what we have in store for you this summer:
- Built-in Snapshot Testing, for testing the long-output of your code with ease
- Describe Blocks, for grouping tests and sharing setup and teardown logic
- Architectural Testing++, even more powerful architectural testing
- Type Coverage Plugin, for measuring the percentage of code that is covered by type declarations
- Drift Plugin, to automatically convert your PHPUnit tests to Pest
Built-in Snapshot Testing
Read the full documentation at: pestphp.com/docs/snapshot-testing
Snapshot testing is a testing technique that allows you to assert that the output of a function or method has not changed. It's a great way to test your codebase and ensure that your code is not changing unexpectedly.
And now, we are proud to announce that Pest will have built-in snapshot testing support. As an example, let's say your "contacts" endpoint outputs a certain HTML every time it runs. You would probably write a test like this:
1it('has a contact page', function () {2 $response = $this->get('/contact');3 4 expect($response)->toMatchSnapshot();5});
The first time you run this test, it will create a snapshot file - at tests/.pest/snapshots
- with the response content. The next time you run the test, it will compare the response with the snapshot file. If the response is different, the test will fail. If the response is the same, the test will pass.
In addition, the given expectation value doesn't have to be a response; it can be anything. For example, you can snapshot an array:
1$array = /** Fetch array somewhere */;2 3expect($array)->toMatchSnapshot();
And of course, you can "rebuild" the snapshots at any time by using the --update-snapshots
option:
1./vendor/bin/pest --update-snapshots
Describe Blocks
Since we released Pest, describe blocks has been one of the most requested features. These are fundamental to any "functional" testing framework, as they allow you to group tests and share setup and teardown logic.
1beforeEach(fn () => $this->user = User::factory()->create()); 2 3describe('auth', function () { 4 beforeEach(fn () => $this->actingAs($this->user)); 5 6 test('cannot login when already logged in', function () { 7 // ... 8 }); 9 10 test('can logout', function () {11 // ...12 });13})->skip(/* Skip the entire describe block */);14 15describe('guest', function () {16 test('can login', function () {17 // ...18 });19 20 // ...21});
Architectural Testing++
Read the full documentation at: pestphp.com/docs/arch-testing
Pest has always been about making testing more enjoyable. Last release, we introduced architectural expectations, which allow you to test your codebase's architecture. This release, we are proud to announce that Pest improves architectural expectations by adding new ones.
1test('controllers')2 ->expect('App\Http\Controllers')3 ->toUseStrictTypes()4 ->toHaveSuffix('Controller') // or toHavePreffix, ...5 ->toBeReadonly()6 ->toBeClasses() // or toBeInterfaces, toBeTraits, ...7 ->classes->not->toBeFinal() // 🌶8 ->classes->toExtendNothing() // or toExtend(Controller::class),9 ->classes->toImplementNothing() // or toImplement(ShouldQueue::class),
Type Coverage Plugin
Read the full documentation at: pestphp.com/docs/type-coverage
As you may know, Pest offers a --coverage
flag that allows you to generate a gorgeous coverage report on the terminal. This report shows you which lines of code are covered by your tests. This is a great way to ensure that your tests are covering all of your code.
To add to this, we are proud to announce that Pest will now have built-in type coverage support. This means that you can now see if your source code is using "types" in every possible place. For example, let's say you have a repository that has the following method:
1public function find($id)2{3 return User::find($id);4}
This method is missing a parameter type and a return type. So, if you run pest --type-coverage, you will see the following output and know that you need to add types to this method:
1...2app/Models\User.php .......................................... 100%3app/Repositories/UserRepository.php .................. pa8, rt8 33%4───────────────────────────────────────────────────────────────────5Total: 91.6 %
In addition, just like regular coverage, you may enforce --min
type coverage percentage. For example, if you run --type-coverage --min=100
, you will see the following output:
1...2app/Models\User.php .......................................................... 100%3app/Repositories/UserRepository.php .................................. pa8, rt8 33%4───────────────────────────────────────────────────────────────────────────────────5 Total: 91.6 %6 ERROR Type coverage below expected: 91.6%. Minimum: 100.0%
Drift Plugin
Read the full documentation at: pestphp.com/docs/migrating-from-phpunit-guide
Yes, you read that right. We are proud to announce that Pest will now have a Laravel shift-like tool called Drift. Drift will allow you to upgrade your PHPUnit tests to Pest tests in a matter of seconds.
So, if you have a test like this:
1<?php 2 3namespace Tests\Unit; 4 5use PHPUnit\Framework\TestCase; 6 7class ExampleTest extends TestCase 8{ 9 public function test_that_true_is_true(): void10 {11 $this->assertTrue(true);12 }13}
You can run ./vendor/bin/pest --drift
and Pest will automatically convert your PHPUnit test to a Pest test:
1test('true is true', function () {2 expect(true)->toBeTrue();3});
Thank you for reading about Pest 2.9's new features! If you're considering a testing framework for your next project, here's why you should give Pest a try: Why Pest →