Testing

There are many different ways to test an Inertia.js app. This page provides a quick overview of the tools available.

End-to-end tests

One popular approach to testing your JavaScript page components, is to use an end-to-end testing tool like Cypress or Laravel Dusk. These are browser automation tools that allow you to run real simulations of your app in the browser. These tests are known to be slower, and sometimes brittle, but since they test your application at the same layer as your end users, they can provide a lot of confidence that your app is working correctly. And, since these tests are run in the browser your JavaScript code is actually executed and tested as well.

Client-side unit tests

Another approach to testing your page components is using a client-side unit testing framework, such as Jest or Mocha. This approach allows you to test your JavaScript page components in isolation using Node.js.

Endpoint tests

In addition to testing your JavaScript page components, you'll also want to test the Inertia responses that come back from your server-side framework. A popular approach to doing this is using endpoint tests, where you make requests to your application and examine the responses. Frameworks, such as Laravel (see here), provide tooling for this.

However, to make this process even easier, in our Laravel adapter we've provided additional HTTP testing tools. Here's an example:

use Inertia\Testing\Assert;

class PodcastsControllerTest extends TestCase
{
    public function test_can_view_podcast()
    {
        $this->get('/podcasts/41')
            ->assertInertia(fn (Assert $page) => $page
                ->component('Podcasts/Show')
                ->has('podcast', fn (Assert $page) => $page
                    ->where('id', $podcast->id)
                    ->where('subject', 'The Laravel Podcast')
                    ->where('description', 'The Laravel Podcast brings you Laravel and PHP development news and discussion.')
                    ->has('seasons', 4)
                    ->has('seasons.4.episodes', 21)
                    ->has('host', fn (Assert $page) => $page
                        ->where('id', 1)
                        ->where('name', 'Matt Stauffer')
                    )
                    ->has('subscribers', 7, fn (Assert $page) => $page
                        ->where('id', 2)
                        ->where('name', 'Claudio Dekker')
                        ->where('platform', 'Apple Podcasts')
                        ->etc()
                        ->missing('email')
                        ->missing('password')
                    )
                )
            );
    }
}
Note, until we're able to add the documentation for these testing helpers to this website, please see the old docs.