In this episode, we kick things off by writing some very basic tests to verify the navigation and homepage visibility depending on whether the user is authenticated or not. Since we're not building out the login and registration functionality just yet, we use tests to simulate both states and ensure our UI behaves as it should.
First, we set up Laravel Fortify, a headless authentication package, to handle our authentication under the hood. We go through installing the package and making any necessary migrations (even though they're minimal at this stage). Before touching any app code, we focus on writing our first feature tests using Pest, Laravel's testing framework.
We write tests to confirm that:
To simulate a signed-in user, we use Laravel's user factory to create a test user and use Pest's actingAs
method. We also touch on managing the database state in tests using Pest's way of auto-refreshing migrations.
As we update our Blade templates to conditionally show navigation items depending on the auth state, we run the tests repeatedly, using test failures to guide our changes. At the end, we experiment with some Pest-specific features to streamline our tests and keep them tidy.
Overall, this episode is a gentle introduction to feature testing in a Laravel app, focusing on authenticated and unauthenticated UI elements. Next up, we'll look at tests focused on the registration process!