Everything We Know about Pest 3
Published on by Paul Redmond
Nuno Maduro was the opening speaker at Laracon US 2024 on Tuesday, unveiling impressive new features for the upcoming Pest v3.0. Pest continues to grow in popularity, being downloaded over 18 million times and is the default testing framework in Laravel as of V11.
Pest v3 takes another important leap forward with three impressive features coming to Pest 3:
- Task management
- Architecture Presets
- Mutation Testing
Let's look at each of these features shared at Laracon US in more detail:
Task Management
Task management lets you track things like assignee, issue number, pull request number, and notes, directly on a test. Tracking tasks allows you to keep a history of work that will always be availabe in your test:
test('some test 1', function () { // ...})->todo(assignee: 'paulredmond', issue: 13); it('returns a successful response', function () { $response = $this->get('/'); $response->assertStatus(200);})->todo( assignee:'paulredmond', issue:11, pr:1, note:'Need to assert the response body.'); // Eric is a closer ☕️test('drives always go in the fairway', function () { // ...})->done(assignee: 'ericlbarnes', issue: 13);
From the command line, you can filter tests by assignee or issue:
$ pest --issue=11$ pest --assignee=paulredmond --pr=1$ pest --assignee=ericlbarnes$ pest --issue=13$ pest --notes # returns tests with notes
Architecture Presets
Architecture presets are a way to quickly use the same architecture rules across multiple projects, and leverage popular presets like rules for Laravel, PHP, a strict PHP preset, or a relaxed preset. These will create more consistency in your code because they'll prevent you from doing things like leaving debug functions like phpinfo(),
dd(),
etc.
You can use these presets using the arch()->preset()
method:
// Ensures PHP functions like die(), phpinfo(), etc. are not usedarch()->preset()->php(); // Ensures functions like eval(), md5(), etc. are not used.arch()->preset()->security(); // Preset to validate things like resourceful controller methods,// enum organization, controller files have a `Controller` suffix, etc.arch()->preset()->laravel(); // Preset to ensure classes are final, strict typing, etc.arch()->preset()->strict(); // Preset to ensure strict types are not used, no final, no private methods.arch()->preset()->relaxed();
Presets speed up development because most rules you'd expect are already defined for you. Here's an example of the security preset failing because of the use of md5()
:
Here's an example of using the Strict preset, demonstrating a class that is not using the final
keyword:
Mutation Testing
When mutation testing ships with Pest v3, it's going to blow your mind. Mark my words, when you run mutation tests on your codebase, this feature is going to make you cry, and then you won't be able to live without it.
Even if your test suite reports 100% coverage, mutation testing will help you find gaps in your tests. It does so by removing code from your implementation, swapping logic around, and poking holes in your code to try to make your tests fail.
Backwards Compatibility
Near the end of his talk, Nuno mentioned that Pest 2 users will be able to upgrade without any backward compatibility breaks. Users should be able to update the package to require v3
and update composer dependencies. This is huge because fixing compatibility breaks in your testing tool can be painful, especially if you have a lot of tests.
The future of Pest is bright with productive tools that level up your testing through mutations, make your application architecture more consistent, and help keep track of assignees and issues directly in your tests.
Which feature are you most excited about? Let us know on your favorite social media app!