Pest 2.9 aka Spicy Summer Release: A PHP Testing Framework Update

We know so many new updates and features were introduced in the last Laracon US 2023 held in Nashville. And Pest 2.9 the highly anticipated “Spicy Summer Release” was one. Before we move towards the update in detail, let’s have an idea about Pest first.

What is Pest and Why Use It?

Pest is a testing PHP Framework developed by Nuno Maduro. While Pest itself is built on top of PHPUnit, the famous and widely adopted PHP testing framework, Pest has a goal to provide a better experience for writing tests. It is believed that Pest is the most sophisticated testing PHP framework and it’s designed to make the testing process enjoyable. It is programmed with the goal of making the tests easy to read and understand, with a code syntax that closely resembles natural human language.

Pest 2.0 was launched on 20th March of 2023 and it is considered a significant release with 7 million downloads at its writing. This remarkable plugin version showcased 80% speed improvement in parallel testing, profiling options, and numerous other features.

Okay, so till now we were referring to the brief of Pest. Let’s come to the actual things we need to discuss here. Pest 2.9 aka “Spicy Summer Release”.

What Does Pest 2.9 Bring to You?

Pest v2.9.0 comes with an array of exciting features. It is not considered as a major release but what the features are presented with it makes it a brilliant one!

Let’s check out…

  • 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: for automatically convert your PHPUnit tests to Pest

1. Built-in Snapshot Testing

Snapshot testing allows you to assert that the output of a function or method has not changed. It’s a great way to test the codebase and ensure that there are no unexpected changes occurring in the code.

And now, the Pest will also 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:

it('has a contact page', function () {
    $response = $this->get('/contact');
 
    expect($response)->toMatchSnapshot();
});

When you run this test for the first time, 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 or else the test will pass.

Additionally, the given expectation value doesn’t have to be a response; it can be anything. For example, you can snapshot an array:

expect($array)->toMatchSnapshot();

You can rebuild the snapshots at any time by using the following option. –update-snapshots

./vendor/bin/pest --update-snapshots

2. Describe Blocks

Describe blocks feature has been one of the most requested features of Pest. These are fundamental to any “functional” testing framework, as they allow you to group tests and share setup and teardown logic.

beforeEach(fn () => $this->user = User::factory()->create());
 
describe('auth', function () {
    beforeEach(fn () => $this->actingAs($this->user));
 
    test('cannot login when already logged in', function () {
        // ...
    });
 
    test('can logout', function () {
        // ...
    });
})->skip(/* Skip the entire describe block */);
 
describe('guest', function () {
    test('can login', function () {
        // ...
    });
 
    // ...
});

3. Architectural Testing++

As always the code testing process seems a little boring than other stuff so to make it more enjoyable Pest introduced architectural expectations in the last release. This allows you to test your codebase’s architecture. In this release, we are proud to announce that Pest improves architectural expectations by adding new ones.

test('controllers')
    ->expect('App\Http\Controllers')
    ->toUseStrictTypes()
    ->toHaveSuffix('Controller') // or toHavePreffix, ...
    ->toBeReadonly()
    ->toBeClasses() // or toBeInterfaces, toBeTraits, ...
    ->classes->not->toBeFinal() //
    ->classes->toExtendNothing() // or toExtend(Controller::class),
    ->classes->toImplementNothing() // or toImplement(ShouldQueue::class),

4. Type Coverage Plugin

You might be familiar with Pest’s –coverage flag, which enables the generation of an appealing coverage report directly on the terminal. This report provides coverage of code lines covered by your tests, ensuring comprehensive test coverage for your codebase.

Excitingly, this time Pest introduces a built-in type coverage support. With this addition, you can now easily determine if your source code is utilizing “types” appropriately in all relevant instances. For instance, let’s consider a repository that has the following method:

public function find($id)
{
    return User::find($id);
}

This method does not contain a parameter type and a return type. So, if you run pest –type-coverage, you will see the below output and know that you need to add types to this method:

...
app/Models\User.php .......................................... 100%
app/Repositories/UserRepository.php .................. pa8, rt8 33%
───────────────────────────────────────────────────────────────────
Total: 91.6 %

Moreover, similar to regular coverage settings, you have the option to set a –min type coverage percentage. For instance, if you run –type-coverage –min=100 will generate the below output:

...
app/Models\User.php .......................................................... 100%
app/Repositories/UserRepository.php .................................. pa8, rt8 33%
───────────────────────────────────────────────────────────────────────────────────
                                                                      Total: 91.6 %
 ERROR  Type coverage below expected: 91.6%. Minimum: 100.0%

Drift Plugin

Pest introduced a Laravel shift-like tool called Drift. Drift will allow you to upgrade your PHPUnit tests to Pest tests in a matter of seconds.

In that case, if you have a test like this:


<?php
 
namespace Tests\Unit;
 
use PHPUnit\Framework\TestCase;
 
class ExampleTest extends TestCase
{
    public function test_that_true_is_true(): void
    {
        $this->assertTrue(true);
    }
}

You can run ./vendor/bin/pest –drift and Pest will convert your PHPUnit test to a Pest test automatically.

 
 
test('true is true', function () {
    expect(true)->toBeTrue();
});

We hope you enjoyed this Pest v2.9.0 and are excited to try these features in your next project.

Hire the right Laravel development company with confidence

Recent Articles

Browse some of our latest articles...

Prev
Next