Join the Mastering Laravel community to level up your skills and get trusted advice

Pest's "Spicy Summer" release

Published on by

Pest's "Spicy Summer" release image

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

Built-in 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:

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

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:

$array = /** Fetch array somewhere */;
 
expect($array)->toMatchSnapshot();

And of course, you can "rebuild" the snapshots at any time by using the --update-snapshots option:

./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.

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 () {
// ...
});
 
// ...
});

Architectural 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.

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),

Type Coverage Plugin

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:

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

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:

...
app/Models\User.php .......................................... 100%
app/Repositories/UserRepository.php .................. pa8, rt8 33%
───────────────────────────────────────────────────────────────────
Total: 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:
 
...
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

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:

<?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 automatically convert your PHPUnit test to a Pest test:

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

Thank you for reading about Pest 2.9's new features!

Nuno Maduro photo

@laravelphp core member — created @pestphp, @laravelzero, collision, larastan, pint, @openai for php, @phpinsights, termwind, and more.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Mastering Laravel

Join the Mastering Laravel community to level up your skills and get trusted advice.

Visit Mastering Laravel
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Join the Mastering Laravel community logo

Join the Mastering Laravel community

Connect with experienced developers in a friendly, noise-free environment. Get insights, share ideas, and find support for your coding challenges. Join us today and elevate your Laravel skills!

Join the Mastering Laravel community
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Streamline Conditional Logic with Laravel's Fluent Conditionable Trait image

Streamline Conditional Logic with Laravel's Fluent Conditionable Trait

Read article
Sublime Text Releases Update With Support for Right Sidebar image

Sublime Text Releases Update With Support for Right Sidebar

Read article
Enhance Email Validation with Laravel's Fluent Email Rule Object image

Enhance Email Validation with Laravel's Fluent Email Rule Object

Read article
Locale-aware Number Parsing in Laravel 12.15 image

Locale-aware Number Parsing in Laravel 12.15

Read article
Handle Fluent Values as Arrays with Laravel's array() Method image

Handle Fluent Values as Arrays with Laravel's array() Method

Read article
Chargebee Starter Kit for Billing in Laravel image

Chargebee Starter Kit for Billing in Laravel

Read article