What's New in Pest 3?
During the Laracon US 2024, Nuno Maduro, the creator of Pest, introduced some exciting new features in the upcoming Pest v3.0. Pest has over 18 million downloads, and it's now the default testing framework for Laravel starting from version 11.
With Pest v3, we can see some major enhancements that make testing even better. Here are the three standout features that Nuno presented during his talk:
- Task Management
- Architectural Preset
- Mutation Testing
Task Management
Task management feature allows us to keep track of the test details, like who is working on the task, issue number, pull request numbers and notes—right within our test cases. This makes it easy to maintain a history of what's been done and what's still needed in your tests.
Here's an example of a test case with task management:
Now from the command line, we can filter these tests by assignee or issue number:
Architectural Preset
Architectural preset in Pest v3 allows us to easily apply consistent coding standards across multiple projects. Whether you're working as a single developer or on a team, small, medium or large scale applications, these presets will help you maintain a consistent code style and structure without repeating same architectural test cases over and over again. These presets help maintain uniformity in our codebase by enforcing specific riles, such as disallowing certain functions or enforcing certain naming conventions. There are currently, 4 different presets available:
- PHP preset
- Security preset
- Laravel preset
- Strict preset
- Relaxed preset
By leveraging these presets, we can prevent common mistakes, like accidentally leaving debug methods like dd() or dump() in our code. Here's an example of how to apply these presets:
Mutation Testing
Mutation testing is a technique that helps us detect bugs in our code by introducing random changes to our codebase. This technique is particularly useful for detecting bugs that are hard to reproduce, such as those that only occur in specific environments or edge cases. Pest v3 introduces mutation testing, which takes our testing to a whole new level by identifying weaknesses in our test suite, even if it shows 100% coverage.
Mutation Testing deliberately changes parts of our code—removing lines, altering logics—to see if our tests catch the changes. If our test fail as expected, great! if not, it reveals gaps in our testing that needs to be addresses. By using mutation testing, we can ensure our test suite is truly robust, not just giving a false sense of security with high coverage percentages.
By default, Pest v3 will run mutation testing on all tests, but we can also filter tests onto specific php files, we can do this by using the mutate method on out Pest.php file:
Backwards Compatibility
Nuno mentioned that Pest v3 is backwards compatible with Pest v2. This means that we can continue to use our existing Pest tests and migrate them to Pest v3 without any issues. The only thing we need to do is update our composer dependencies to use Pest v3.
Conclusion
With features like mutation testing to strengthen our test coverage, architectural presets to maintain consistency in our codebase, and task management to keep track of our test cases, Pest v3 is taking testing to a whole new level. I'm excited to see what other features we can expect to see in the future. Thank you for joining me on this journey, and I hope you enjoyed this post.
Your feedback is valuable to me, so if you'd like to see more content like this in the future, please don't hesitate to let me know.
Keep coding, keep exploring, and keep inspiring. 🐼