Skip to content

Testing

Advanced

Testing

There are many different ways to test an Inertia application. 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 Pest. 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; however, 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 to use a client-side unit testing framework, such as Vitest, 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 will likely want to also test the Inertia responses that are returned by 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. Laravel provides tooling for executing these types of tests.

However, to make this process even easier, Inertia's Laravel adapters provide additional HTTP testing tools. Let's take a look at an example.

1use laravel::testing::prelude::*;
2 
3#[laravel::test]
4async fn can_view_podcast(app: Application) {
5 app.get("/podcasts/41")
6 .await
7 .assert_inertia()
8 .component("Podcasts/Show")
9 .scope("podcast", |page| {
10 page.equals("id", podcast.id)
11 .equals("subject", "The Laravel Podcast")
12 .equals(
13 "description",
14 "The Laravel Podcast brings you Laravel development news and discussion.",
15 )
16 .has_count("seasons", 4)
17 .has_count("seasons.4.episodes", 21)
18 .scope("host", |page| {
19 page.equals("id", 1).equals("name", "Matt Stauffer")
20 })
21 .has_count("subscribers", 7)
22 .first("subscribers", |page| {
23 page.equals("id", 2)
24 .equals("name", "Claudio Dekker")
25 .equals("platform", "Apple Podcasts")
26 .missing("email")
27 .missing("password")
28 .etc()
29 })
30 });
31}
1use laravel::testing::prelude::*;
2 
3#[laravel::test]
4async fn can_view_podcast(app: Application) {
5 app.get("/podcasts/41")
6 .await
7 .assert_inertia()
8 .component("Podcasts/Show")
9 .scope("podcast", |page| {
10 page.equals("id", podcast.id)
11 .equals("subject", "The Laravel Podcast")
12 .equals(
13 "description",
14 "The Laravel Podcast brings you Laravel development news and discussion.",
15 )
16 .has_count("seasons", 4)
17 .has_count("seasons.4.episodes", 21)
18 .scope("host", |page| {
19 page.equals("id", 1).equals("name", "Matt Stauffer")
20 })
21 .has_count("subscribers", 7)
22 .first("subscribers", |page| {
23 page.equals("id", 2)
24 .equals("name", "Claudio Dekker")
25 .equals("platform", "Apple Podcasts")
26 .missing("email")
27 .missing("password")
28 .etc()
29 })
30 });
31}
1package feature_test
2 
3import (
4 "testing"
5 
6 "go.laravel.com/webtest"
7 
8 "app/tests"
9)
10 
11func TestCanViewPodcast(t *testing.T) {
12 client := tests.New(t)
13 
14 client.Get("/podcasts/41").
15 AssertInertia("Podcasts/Show").
16 AssertInertiaMatches(func(page *webtest.JSON) {
17 page.Scope("podcast", func(page *webtest.JSON) {
18 page.Equals("id", podcast.ID).
19 Equals("subject", "The Laravel Podcast").
20 Equals("description", "The Laravel Podcast brings you Laravel and PHP development news and discussion.").
21 Count("seasons", 4).
22 Count("seasons.4.episodes", 21).
23 Scope("host", func(page *webtest.JSON) {
24 page.Equals("id", 1).Equals("name", "Matt Stauffer")
25 }).
26 Count("subscribers", 7).
27 First("subscribers", func(page *webtest.JSON) {
28 page.Equals("id", 2).
29 Equals("name", "Claudio Dekker").
30 Equals("platform", "Apple Podcasts").
31 Missing("email").
32 Missing("password").
33 Etc()
34 })
35 })
36 })
37}
1package feature_test
2 
3import (
4 "testing"
5 
6 "go.laravel.com/webtest"
7 
8 "app/tests"
9)
10 
11func TestCanViewPodcast(t *testing.T) {
12 client := tests.New(t)
13 
14 client.Get("/podcasts/41").
15 AssertInertia("Podcasts/Show").
16 AssertInertiaMatches(func(page *webtest.JSON) {
17 page.Scope("podcast", func(page *webtest.JSON) {
18 page.Equals("id", podcast.ID).
19 Equals("subject", "The Laravel Podcast").
20 Equals("description", "The Laravel Podcast brings you Laravel and PHP development news and discussion.").
21 Count("seasons", 4).
22 Count("seasons.4.episodes", 21).
23 Scope("host", func(page *webtest.JSON) {
24 page.Equals("id", 1).Equals("name", "Matt Stauffer")
25 }).
26 Count("subscribers", 7).
27 First("subscribers", func(page *webtest.JSON) {
28 page.Equals("id", 2).
29 Equals("name", "Claudio Dekker").
30 Equals("platform", "Apple Podcasts").
31 Missing("email").
32 Missing("password").
33 Etc()
34 })
35 })
36 })
37}
1use Inertia\Testing\AssertableInertia as Assert;
2 
3class PodcastsControllerTest extends TestCase
4{
5 public function test_can_view_podcast()
6 {
7 $this->get('/podcasts/41')
8 ->assertInertia(fn (Assert $page) => $page
9 ->component('Podcasts/Show')
10 ->has('podcast', fn (Assert $page) => $page
11 ->where('id', $podcast->id)
12 ->where('subject', 'The Laravel Podcast')
13 ->where('description', 'The Laravel Podcast brings you Laravel and PHP development news and discussion.')
14 ->has('seasons', 4)
15 ->has('seasons.4.episodes', 21)
16 ->has('host', fn (Assert $page) => $page
17 ->where('id', 1)
18 ->where('name', 'Matt Stauffer')
19 )
20 ->has('subscribers', 7, fn (Assert $page) => $page
21 ->where('id', 2)
22 ->where('name', 'Claudio Dekker')
23 ->where('platform', 'Apple Podcasts')
24 ->etc()
25 ->missing('email')
26 ->missing('password')
27 )
28 )
29 );
30 }
31}
1use Inertia\Testing\AssertableInertia as Assert;
2 
3class PodcastsControllerTest extends TestCase
4{
5 public function test_can_view_podcast()
6 {
7 $this->get('/podcasts/41')
8 ->assertInertia(fn (Assert $page) => $page
9 ->component('Podcasts/Show')
10 ->has('podcast', fn (Assert $page) => $page
11 ->where('id', $podcast->id)
12 ->where('subject', 'The Laravel Podcast')
13 ->where('description', 'The Laravel Podcast brings you Laravel and PHP development news and discussion.')
14 ->has('seasons', 4)
15 ->has('seasons.4.episodes', 21)
16 ->has('host', fn (Assert $page) => $page
17 ->where('id', 1)
18 ->where('name', 'Matt Stauffer')
19 )
20 ->has('subscribers', 7, fn (Assert $page) => $page
21 ->where('id', 2)
22 ->where('name', 'Claudio Dekker')
23 ->where('platform', 'Apple Podcasts')
24 ->etc()
25 ->missing('email')
26 ->missing('password')
27 )
28 )
29 );
30 }
31}

As you can see in the example above, you may use these assertion methods to assert against the content of the data provided to the Inertia response. In addition, you may assert that array data has a given length as well as scope your assertions.

You may also retrieve the props returned in the response directly. Nested properties are supported using "dot" notation.

1let page = app.get("/podcasts/41").await.assert_inertia();
2 
3// Returns all props...
4let props = &page.page().props;
5 
6// Returns a specific prop...
7let podcast = page.prop("podcast");
8 
9// Returns a nested prop using "dot" notation...
10let podcast_id = page.prop("podcast.id");
1let page = app.get("/podcasts/41").await.assert_inertia();
2 
3// Returns all props...
4let props = &page.page().props;
5 
6// Returns a specific prop...
7let podcast = page.prop("podcast");
8 
9// Returns a nested prop using "dot" notation...
10let podcast_id = page.prop("podcast.id");
1res := client.Get("/podcasts/41")
2 
3// Returns a specific prop...
4podcast := res.Prop[models.Podcast]("podcast")
5 
6// Returns a nested prop using "dot" notation...
7podcastID := res.Prop[int64]("podcast.id")
1res := client.Get("/podcasts/41")
2 
3// Returns a specific prop...
4podcast := res.Prop[models.Podcast]("podcast")
5 
6// Returns a nested prop using "dot" notation...
7podcastID := res.Prop[int64]("podcast.id")
1$response = $this->get('/podcasts/41');
2 
3// Returns all props...
4$response->inertiaProps();
5 
6// Returns a specific prop...
7$response->inertiaProps('podcast');
8 
9// Returns a nested prop using "dot" notation...
10$response->inertiaProps('podcast.id');
1$response = $this->get('/podcasts/41');
2 
3// Returns all props...
4$response->inertiaProps();
5 
6// Returns a specific prop...
7$response->inertiaProps('podcast');
8 
9// Returns a nested prop using "dot" notation...
10$response->inertiaProps('podcast.id');

Let's dig into assert_inertiaAssertInertia and AssertInertiaMatchesassertInertia and the available assertions in detail. First, to assert that the Inertia response has a property, you may use the hasHashas method.

1response
2 .assert_inertia()
3 // Checking a root-level property...
4 .has("podcasts")
5 // Checking nested properties using "dot" notation...
6 .has("podcast.subscribers");
1response
2 .assert_inertia()
3 // Checking a root-level property...
4 .has("podcasts")
5 // Checking nested properties using "dot" notation...
6 .has("podcast.subscribers");
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 // Checking a root-level property...
3 page.Has("podcasts").
4 // Checking nested properties using "dot" notation...
5 Has("podcast.subscribers")
6})
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 // Checking a root-level property...
3 page.Has("podcasts").
4 // Checking nested properties using "dot" notation...
5 Has("podcast.subscribers")
6})
1$response->assertInertia(fn (Assert $page) => $page
2 // Checking a root-level property...
3 ->has('podcasts')
4 
5 // Checking nested properties using "dot" notation...
6 ->has('podcast.subscribers')
7);
1$response->assertInertia(fn (Assert $page) => $page
2 // Checking a root-level property...
3 ->has('podcasts')
4 
5 // Checking nested properties using "dot" notation...
6 ->has('podcast.subscribers')
7);

To assert that an Inertia property has a specified number of items, use has_countuse Countprovide the expected size as the second argument to has.

1response
2 .assert_inertia()
3 // Checking if a root-level property has 7 items...
4 .has_count("podcasts", 7)
5 // Checking nested properties using "dot" notation...
6 .has_count("podcast.subscribers", 7);
1response
2 .assert_inertia()
3 // Checking if a root-level property has 7 items...
4 .has_count("podcasts", 7)
5 // Checking nested properties using "dot" notation...
6 .has_count("podcast.subscribers", 7);
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 // Checking if a root-level property has 7 items...
3 page.Count("podcasts", 7).
4 // Checking nested properties using "dot" notation...
5 Count("podcast.subscribers", 7)
6})
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 // Checking if a root-level property has 7 items...
3 page.Count("podcasts", 7).
4 // Checking nested properties using "dot" notation...
5 Count("podcast.subscribers", 7)
6})
1$response->assertInertia(fn (Assert $page) => $page
2 // Checking if a root-level property has 7 items...
3 ->has('podcasts', 7)
4 
5 // Checking nested properties using "dot" notation...
6 ->has('podcast.subscribers', 7)
7);
1$response->assertInertia(fn (Assert $page) => $page
2 // Checking if a root-level property has 7 items...
3 ->has('podcasts', 7)
4 
5 // Checking nested properties using "dot" notation...
6 ->has('podcast.subscribers', 7)
7);

You may also scope properties (using scopeusing Scopehas with a closure) in order to lessen repetition when asserting against nested properties.

1response.assert_inertia().scope("message", |page| {
2 // We can now continue chaining methods...
3 page.has("subject")
4 .has_count("comments", 5)
5 // And can even create a deeper scope using "dot" notation...
6 .scope("comments.0", |page| {
7 page.has("body")
8 .has_count("files", 1)
9 .scope("files.0", |page| page.has("url"))
10 })
11});
1response.assert_inertia().scope("message", |page| {
2 // We can now continue chaining methods...
3 page.has("subject")
4 .has_count("comments", 5)
5 // And can even create a deeper scope using "dot" notation...
6 .scope("comments.0", |page| {
7 page.has("body")
8 .has_count("files", 1)
9 .scope("files.0", |page| page.has("url"))
10 })
11});
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 // Creating a single-level property scope...
3 page.Scope("message", func(page *webtest.JSON) {
4 // We can now continue chaining methods...
5 page.Has("subject").
6 Count("comments", 5).
7 // And can even create a deeper scope using "dot" notation...
8 Scope("comments.0", func(page *webtest.JSON) {
9 page.Has("body").
10 Count("files", 1).
11 Scope("files.0", func(page *webtest.JSON) {
12 page.Has("url")
13 })
14 })
15 })
16})
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 // Creating a single-level property scope...
3 page.Scope("message", func(page *webtest.JSON) {
4 // We can now continue chaining methods...
5 page.Has("subject").
6 Count("comments", 5).
7 // And can even create a deeper scope using "dot" notation...
8 Scope("comments.0", func(page *webtest.JSON) {
9 page.Has("body").
10 Count("files", 1).
11 Scope("files.0", func(page *webtest.JSON) {
12 page.Has("url")
13 })
14 })
15 })
16})
1$response->assertInertia(fn (Assert $page) => $page
2 // Creating a single-level property scope...
3 ->has('message', fn (Assert $page) => $page
4 // We can now continue chaining methods...
5 ->has('subject')
6 ->has('comments', 5)
7 
8 // And can even create a deeper scope using "dot" notation...
9 ->has('comments.0', fn (Assert $page) => $page
10 ->has('body')
11 ->has('files', 1)
12 ->has('files.0', fn (Assert $page) => $page
13 ->has('url')
14 )
15 )
16 )
17);
1$response->assertInertia(fn (Assert $page) => $page
2 // Creating a single-level property scope...
3 ->has('message', fn (Assert $page) => $page
4 // We can now continue chaining methods...
5 ->has('subject')
6 ->has('comments', 5)
7 
8 // And can even create a deeper scope using "dot" notation...
9 ->has('comments.0', fn (Assert $page) => $page
10 ->has('body')
11 ->has('files', 1)
12 ->has('files.0', fn (Assert $page) => $page
13 ->has('url')
14 )
15 )
16 )
17);

When scoping into Inertia properties that are arrays or collections, you may also assert that a specified number of items are present in addition to scoping into the first item.

1response
2 .assert_inertia()
3 // Assert that there are 5 comments and scope into the first comment...
4 .has_count("comments", 5)
5 .first("comments", |page| {
6 page.has("body")
7 // ...
8 .etc()
9 });
1response
2 .assert_inertia()
3 // Assert that there are 5 comments and scope into the first comment...
4 .has_count("comments", 5)
5 .first("comments", |page| {
6 page.has("body")
7 // ...
8 .etc()
9 });
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 // Assert that there are 5 comments and scope into the first comment...
3 page.Count("comments", 5).
4 First("comments", func(page *webtest.JSON) {
5 page.Has("body").
6 // ...
7 Etc()
8 })
9})
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 // Assert that there are 5 comments and scope into the first comment...
3 page.Count("comments", 5).
4 First("comments", func(page *webtest.JSON) {
5 page.Has("body").
6 // ...
7 Etc()
8 })
9})
1$response->assertInertia(fn (Assert $page) => $page
2 // Assert that there are 5 comments and automatically scope into the first comment...
3 ->has('comments', 5, fn (Assert $page) => $page
4 ->has('body')
5 // ...
6 )
7);
1$response->assertInertia(fn (Assert $page) => $page
2 // Assert that there are 5 comments and automatically scope into the first comment...
3 ->has('comments', 5, fn (Assert $page) => $page
4 ->has('body')
5 // ...
6 )
7);

To assert that an Inertia property has an expected value, use equalsEquals (or AssertProp directly on the response)where.

1response.assert_inertia().scope("message", |page| {
2 // Assert that the subject prop matches the given message...
3 page.equals("subject", "This is an example message")
4 // Or, assert against deeply nested values...
5 .equals("comments.0.files.0.name", "example-attachment.pdf")
6});
1response.assert_inertia().scope("message", |page| {
2 // Assert that the subject prop matches the given message...
3 page.equals("subject", "This is an example message")
4 // Or, assert against deeply nested values...
5 .equals("comments.0.files.0.name", "example-attachment.pdf")
6});
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 page.Scope("message", func(page *webtest.JSON) {
3 // Assert that the subject prop matches the given message...
4 page.Equals("subject", "This is an example message").
5 // Or, assert against deeply nested values...
6 Equals("comments.0.files.0.name", "example-attachment.pdf")
7 })
8})
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 page.Scope("message", func(page *webtest.JSON) {
3 // Assert that the subject prop matches the given message...
4 page.Equals("subject", "This is an example message").
5 // Or, assert against deeply nested values...
6 Equals("comments.0.files.0.name", "example-attachment.pdf")
7 })
8})
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('message', fn (Assert $page) => $page
3 // Assert that the subject prop matches the given message...
4 ->where('subject', 'This is an example message')
5 
6 // Or, assert against deeply nested values...
7 ->where('comments.0.files.0.name', 'example-attachment.pdf')
8 )
9);
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('message', fn (Assert $page) => $page
3 // Assert that the subject prop matches the given message...
4 ->where('subject', 'This is an example message')
5 
6 // Or, assert against deeply nested values...
7 ->where('comments.0.files.0.name', 'example-attachment.pdf')
8 )
9);

Inertia's testing methods will automatically fail when you haven't interacted with all of the props in a scope. While this is generally useful, you might run into situations where you're working with unreliable data (such as from an external feed), or with data that you really don't want to interact with in order to keep your test simple. For these situations, the etcEtcetc method exists.

1response.assert_inertia().scope("message", |page| {
2 page.has("subject").has("comments").etc()
3});
1response.assert_inertia().scope("message", |page| {
2 page.has("subject").has("comments").etc()
3});
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 page.Scope("message", func(page *webtest.JSON) {
3 page.Has("subject").
4 Has("comments").
5 Etc()
6 })
7})
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 page.Scope("message", func(page *webtest.JSON) {
3 page.Has("subject").
4 Has("comments").
5 Etc()
6 })
7})
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('message', fn (Assert $page) => $page
3 ->has('subject')
4 ->has('comments')
5 ->etc()
6 )
7);
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('message', fn (Assert $page) => $page
3 ->has('subject')
4 ->has('comments')
5 ->etc()
6 )
7);

The missingMissing (or AssertPropMissing directly on the response)missing method is the exact opposite of the hasHashas method, ensuring that the property does not exist. This method makes a great companion to the etcEtcetc method.

1response.assert_inertia().scope("message", |page| {
2 page.has("subject").missing("published_at").etc()
3});
1response.assert_inertia().scope("message", |page| {
2 page.has("subject").missing("published_at").etc()
3});
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 page.Scope("message", func(page *webtest.JSON) {
3 page.Has("subject").
4 Missing("published_at").
5 Etc()
6 })
7})
1res.AssertInertiaMatches(func(page *webtest.JSON) {
2 page.Scope("message", func(page *webtest.JSON) {
3 page.Has("subject").
4 Missing("published_at").
5 Etc()
6 })
7})
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('message', fn (Assert $page) => $page
3 ->has('subject')
4 ->missing('published_at')
5 ->etc()
6 )
7);
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('message', fn (Assert $page) => $page
3 ->has('subject')
4 ->missing('published_at')
5 ->etc()
6 )
7);

Testing Partial Reloads

You may use reload_only and reload_exceptReloadOnly and ReloadExceptreloadOnly and reloadExcept to test how your application responds to partial reloads. These methods perform a follow-up request and allow you to make assertions against the response.

1let page = response.assert_inertia();
2 
3page.has("orders").missing("statuses");
4 
5page.reload_only(&["statuses"])
6 .await
7 .missing("orders")
8 .has_count("statuses", 5);
1let page = response.assert_inertia();
2 
3page.has("orders").missing("statuses");
4 
5page.reload_only(&["statuses"])
6 .await
7 .missing("orders")
8 .has_count("statuses", 5);
1res.AssertPropMissing("statuses").
2 AssertInertiaMatches(func(page *webtest.JSON) {
3 page.Has("orders")
4 })
5 
6res.ReloadOnly("statuses").
7 AssertPropMissing("orders").
8 AssertInertiaMatches(func(page *webtest.JSON) {
9 page.Count("statuses", 5)
10 })
1res.AssertPropMissing("statuses").
2 AssertInertiaMatches(func(page *webtest.JSON) {
3 page.Has("orders")
4 })
5 
6res.ReloadOnly("statuses").
7 AssertPropMissing("orders").
8 AssertInertiaMatches(func(page *webtest.JSON) {
9 page.Count("statuses", 5)
10 })
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('orders')
3 ->missing('statuses')
4 ->reloadOnly('statuses', fn (Assert $reload) => $reload
5 ->missing('orders')
6 ->has('statuses', 5)
7 )
8);
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('orders')
3 ->missing('statuses')
4 ->reloadOnly('statuses', fn (Assert $reload) => $reload
5 ->missing('orders')
6 ->has('statuses', 5)
7 )
8);

Instead of passing a single prop as a string, you may also pass an array of props to reloadOnly or reloadExcept.

Testing Deferred Props

You may use load_deferred_propsLoadDeferredPropsloadDeferredProps to test how your application responds to deferred props. These methods perform a follow-up request to load the deferred props and allow you to make assertions against the response.

1let page = response.assert_inertia();
2 
3page.has("users")
4 .has("roles")
5 .missing("permissions"); // Deferred prop not in initial response
6 
7page.load_deferred_props()
8 .await
9 .has("permissions")
10 .equals("permissions.0.name", "edit users");
1let page = response.assert_inertia();
2 
3page.has("users")
4 .has("roles")
5 .missing("permissions"); // Deferred prop not in initial response
6 
7page.load_deferred_props()
8 .await
9 .has("permissions")
10 .equals("permissions.0.name", "edit users");
1res.AssertDeferred("permissions").
2 AssertPropMissing("permissions"). // Deferred prop not in initial response
3 AssertInertiaMatches(func(page *webtest.JSON) {
4 page.Has("users").Has("roles")
5 })
6 
7res.LoadDeferredProps().
8 AssertProp("permissions.0.name", "edit users")
1res.AssertDeferred("permissions").
2 AssertPropMissing("permissions"). // Deferred prop not in initial response
3 AssertInertiaMatches(func(page *webtest.JSON) {
4 page.Has("users").Has("roles")
5 })
6 
7res.LoadDeferredProps().
8 AssertProp("permissions.0.name", "edit users")
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('users')
3 ->has('roles')
4 ->missing('permissions') // Deferred prop not in initial response
5 ->loadDeferredProps(fn (Assert $reload) => $reload
6 ->has('permissions')
7 ->where('permissions.0.name', 'edit users')
8 )
9);
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('users')
3 ->has('roles')
4 ->missing('permissions') // Deferred prop not in initial response
5 ->loadDeferredProps(fn (Assert $reload) => $reload
6 ->has('permissions')
7 ->where('permissions.0.name', 'edit users')
8 )
9);

You may also load specific deferred prop groups by calling load_deferred_groupscalling LoadDeferredGroupspassing the group name to loadDeferredProps.

1let page = response.assert_inertia();
2 
3page.has("users").missing("teams").missing("projects");
4 
5page.load_deferred_groups(&["attributes"])
6 .await
7 .has_count("teams", 5)
8 .has("projects")
9 .missing("permissions"); // Different group
1let page = response.assert_inertia();
2 
3page.has("users").missing("teams").missing("projects");
4 
5page.load_deferred_groups(&["attributes"])
6 .await
7 .has_count("teams", 5)
8 .has("projects")
9 .missing("permissions"); // Different group
1res.AssertDeferredIn("attributes", "teams", "projects").
2 AssertPropMissing("teams").
3 AssertPropMissing("projects")
4 
5res.LoadDeferredGroups("attributes").
6 AssertPropMissing("permissions"). // Different group
7 AssertInertiaMatches(func(page *webtest.JSON) {
8 page.Count("teams", 5).Has("projects")
9 })
1res.AssertDeferredIn("attributes", "teams", "projects").
2 AssertPropMissing("teams").
3 AssertPropMissing("projects")
4 
5res.LoadDeferredGroups("attributes").
6 AssertPropMissing("permissions"). // Different group
7 AssertInertiaMatches(func(page *webtest.JSON) {
8 page.Count("teams", 5).Has("projects")
9 })
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('users')
3 ->missing('teams')
4 ->missing('projects')
5 ->loadDeferredProps('attributes', fn (Assert $reload) => $reload
6 ->has('teams', 5)
7 ->has('projects')
8 ->missing('permissions') // Different group
9 )
10);
1$response->assertInertia(fn (Assert $page) => $page
2 ->has('users')
3 ->missing('teams')
4 ->missing('projects')
5 ->loadDeferredProps('attributes', fn (Assert $reload) => $reload
6 ->has('teams', 5)
7 ->has('projects')
8 ->missing('permissions') // Different group
9 )
10);

You may also load multiple deferred prop groups at once.

1response
2 .assert_inertia()
3 .load_deferred_groups(&["default", "attributes"])
4 .await
5 .has("permissions")
6 .has("teams")
7 .has("projects");
1response
2 .assert_inertia()
3 .load_deferred_groups(&["default", "attributes"])
4 .await
5 .has("permissions")
6 .has("teams")
7 .has("projects");
1res.LoadDeferredGroups("default", "attributes").
2 AssertInertiaMatches(func(page *webtest.JSON) {
3 page.Has("permissions").
4 Has("teams").
5 Has("projects")
6 })
1res.LoadDeferredGroups("default", "attributes").
2 AssertInertiaMatches(func(page *webtest.JSON) {
3 page.Has("permissions").
4 Has("teams").
5 Has("projects")
6 })
1$response->assertInertia(fn (Assert $page) => $page
2 ->loadDeferredProps(['default', 'attributes'], fn (Assert $reload) => $reload
3 ->has('permissions')
4 ->has('teams')
5 ->has('projects')
6 )
7);
1$response->assertInertia(fn (Assert $page) => $page
2 ->loadDeferredProps(['default', 'attributes'], fn (Assert $reload) => $reload
3 ->has('permissions')
4 ->has('teams')
5 ->has('projects')
6 )
7);

Testing Flash Data

You may assert against flash data in your Inertia responses using has_flash, flash, and missing_flashAssertFlash and AssertFlashMissinghasFlash and missingFlash.

1response
2 .assert_inertia()
3 // Assert flash data exists...
4 .has_flash("message")
5 // Assert flash data has a specific value...
6 .flash("message", "Item saved!")
7 // Nested values are supported using "dot" notation...
8 .flash("notification.type", "success")
9 // Assert flash data does not exist...
10 .missing_flash("error");
1response
2 .assert_inertia()
3 // Assert flash data exists...
4 .has_flash("message")
5 // Assert flash data has a specific value...
6 .flash("message", "Item saved!")
7 // Nested values are supported using "dot" notation...
8 .flash("notification.type", "success")
9 // Assert flash data does not exist...
10 .missing_flash("error");
1res.
2 // Assert flash data has a specific value...
3 AssertFlash("message", "Item saved!").
4 // Nested values are supported using "dot" notation...
5 AssertFlash("notification.type", "success").
6 // Assert flash data does not exist...
7 AssertFlashMissing("error")
1res.
2 // Assert flash data has a specific value...
3 AssertFlash("message", "Item saved!").
4 // Nested values are supported using "dot" notation...
5 AssertFlash("notification.type", "success").
6 // Assert flash data does not exist...
7 AssertFlashMissing("error")
1$response->assertInertia(fn (Assert $page) => $page
2 // Assert flash data exists...
3 ->hasFlash('message')
4 
5 // Assert flash data has a specific value...
6 ->hasFlash('message', 'Item saved!')
7 
8 // Nested values are supported using "dot" notation...
9 ->hasFlash('notification.type', 'success')
10 
11 // Assert flash data does not exist...
12 ->missingFlash('error')
13);
1$response->assertInertia(fn (Assert $page) => $page
2 // Assert flash data exists...
3 ->hasFlash('message')
4 
5 // Assert flash data has a specific value...
6 ->hasFlash('message', 'Item saved!')
7 
8 // Nested values are supported using "dot" notation...
9 ->hasFlash('notification.type', 'success')
10 
11 // Assert flash data does not exist...
12 ->missingFlash('error')
13);

Redirect Responses

The page assertions above only work on rendered Inertia page responses. For redirect responses, follow the redirect with follow_redirects() and assert on the resulting Inertia pagethe test client follows redirects automatically, so you may chain AssertRedirect and flash assertions directly on the responseyou may use assertInertiaFlash and assertInertiaFlashMissing directly on the test response to assert against the session's flash data.

1app.post("/users")
2 .follow_redirects()
3 .await
4 .assert_inertia()
5 .has_flash("message")
6 .flash("message", "User created!")
7 .flash("notification.type", "success")
8 .missing_flash("error");
1app.post("/users")
2 .follow_redirects()
3 .await
4 .assert_inertia()
5 .has_flash("message")
6 .flash("message", "User created!")
7 .flash("notification.type", "success")
8 .missing_flash("error");
1client.Post("/users", nil).
2 AssertRedirect("/dashboard").
3 AssertFlash("message", "User created!").
4 AssertFlash("notification.type", "success").
5 AssertFlashMissing("error")
1client.Post("/users", nil).
2 AssertRedirect("/dashboard").
3 AssertFlash("message", "User created!").
4 AssertFlash("notification.type", "success").
5 AssertFlashMissing("error")
1$response = $this->post('/users');
2 
3$response->assertRedirect('/dashboard')
4 ->assertInertiaFlash('message')
5 ->assertInertiaFlash('message', 'User created!')
6 ->assertInertiaFlash('notification.type', 'success')
7 ->assertInertiaFlashMissing('error');
1$response = $this->post('/users');
2 
3$response->assertRedirect('/dashboard')
4 ->assertInertiaFlash('message')
5 ->assertInertiaFlash('message', 'User created!')
6 ->assertInertiaFlash('notification.type', 'success')
7 ->assertInertiaFlashMissing('error');

Disabling SSR During Tests

Your application may have SSR enabled for development and production, but you might not need it during testing. New applications already set ("INERTIA_SSR", "false") in src/testing.rs.New applications already set "INERTIA_SSR": "false" in tests/tests.go.Set INERTIA_SSR_ENABLED to false in your phpunit.xml.

src/testing.rs
1use laravel::foundation::ApplicationBuilder;
2 
3pub fn configure() -> ApplicationBuilder {
4 crate::configure()
5 .for_testing(env!("CARGO_MANIFEST_DIR"))
6 .with_envs([
7 ("INERTIA_SSR", "false"),
8 // ...
9 ])
10 .refresh_database()
11}
1use laravel::foundation::ApplicationBuilder;
2 
3pub fn configure() -> ApplicationBuilder {
4 crate::configure()
5 .for_testing(env!("CARGO_MANIFEST_DIR"))
6 .with_envs([
7 ("INERTIA_SSR", "false"),
8 // ...
9 ])
10 .refresh_database()
11}
tests/tests.go
1func New(t testing.TB, with ...laraveltest.Option) *laraveltest.Client {
2 with = append([]laraveltest.Option{
3 laraveltest.Env(map[string]string{
4 "INERTIA_SSR": "false",
5 // ...
6 }),
7 laraveltest.RefreshDatabase,
8 }, with...)
9 
10 return laraveltest.New(t, bootstrap.Options(), with...)
11}
1func New(t testing.TB, with ...laraveltest.Option) *laraveltest.Client {
2 with = append([]laraveltest.Option{
3 laraveltest.Env(map[string]string{
4 "INERTIA_SSR": "false",
5 // ...
6 }),
7 laraveltest.RefreshDatabase,
8 }, with...)
9 
10 return laraveltest.New(t, bootstrap.Options(), with...)
11}
phpunit.xml
1<env name="INERTIA_SSR_ENABLED" value="false" />
1<env name="INERTIA_SSR_ENABLED" value="false" />

You may alternatively use the Inertia::disableSsr() method in your test base class.

1use Inertia\Inertia;
2 
3abstract class TestCase extends BaseTestCase
4{
5 protected function setUp(): void
6 {
7 parent::setUp();
8 
9 Inertia::disableSsr();
10 }
11}
1use Inertia\Inertia;
2 
3abstract class TestCase extends BaseTestCase
4{
5 protected function setUp(): void
6 {
7 parent::setUp();
8 
9 Inertia::disableSsr();
10 }
11}

A boolean or closure may be passed to disable SSR conditionally. This is typically called from a service provider.

1// Evaluated immediately...
2Inertia::disableSsr(app()->runningUnitTests());
3 
4// Evaluated lazily...
5Inertia::disableSsr(fn () => app()->runningUnitTests());
1// Evaluated immediately...
2Inertia::disableSsr(app()->runningUnitTests());
3 
4// Evaluated lazily...
5Inertia::disableSsr(fn () => app()->runningUnitTests());