In this episode, we get hands-on and create our very first custom function for Pest. We start by looking at where and how custom functions live inside pest.php
, and why you might want to define your own to simplify your tests.
For this one, we're building an actingAs
function to clean up our authentication logic in tests. Rather than writing $this->actingAs($user)
everywhere, the custom function lets us skip the annoying $this->
and just pass in the user directly—making the tests a bit cleaner (and saving our fingers a little typing!).
We walk through how to type-hint the user parameter for flexibility, explain how Pest's internal magic lets us chain things as we need, and run the tests to prove it all works. Finally, we also show how this new helper makes our tests EVEN tidier by allowing shorter and easier-to-read assertions.
At the end, we wrap up by previewing the next episode, where we'll explore creating a whole test with just a single line. It's all about keeping our codebase concise and clean!