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 Inertia\Testing\AssertableInertia as Assert;23class PodcastsControllerTest extends TestCase4{5 public function test_can_view_podcast()6 {7 $this->get('/podcasts/41')8 ->assertInertia(fn (Assert $page) => $page9 ->component('Podcasts/Show')10 ->has('podcast', fn (Assert $page) => $page11 ->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) => $page17 ->where('id', 1)18 ->where('name', 'Matt Stauffer')19 )20 ->has('subscribers', 7, fn (Assert $page) => $page21 ->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;23class PodcastsControllerTest extends TestCase4{5 public function test_can_view_podcast()6 {7 $this->get('/podcasts/41')8 ->assertInertia(fn (Assert $page) => $page9 ->component('Podcasts/Show')10 ->has('podcast', fn (Assert $page) => $page11 ->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) => $page17 ->where('id', 1)18 ->where('name', 'Matt Stauffer')19 )20 ->has('subscribers', 7, fn (Assert $page) => $page21 ->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 laravel::testing::prelude::*;23#[laravel::test]4async fn can_view_podcast(app: Application) {5 app.get("/podcasts/41")6 .await7 .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::*;23#[laravel::test]4async fn can_view_podcast(app: Application) {5 app.get("/podcasts/41")6 .await7 .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}
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.
1$response = $this->get('/podcasts/41');23// Returns all props...4$response->inertiaProps();56// Returns a specific prop...7$response->inertiaProps('podcast');89// Returns a nested prop using "dot" notation...10$response->inertiaProps('podcast.id');1$response = $this->get('/podcasts/41');23// Returns all props...4$response->inertiaProps();56// Returns a specific prop...7$response->inertiaProps('podcast');89// Returns a nested prop using "dot" notation...10$response->inertiaProps('podcast.id');
1let page = app.get("/podcasts/41").await.assert_inertia();23// Returns all props...4let props = &page.page().props;56// Returns a specific prop...7let podcast = page.prop("podcast");89// Returns a nested prop using "dot" notation...10let podcast_id = page.prop("podcast.id");1let page = app.get("/podcasts/41").await.assert_inertia();23// Returns all props...4let props = &page.page().props;56// Returns a specific prop...7let podcast = page.prop("podcast");89// Returns a nested prop using "dot" notation...10let podcast_id = page.prop("podcast.id");
Let's dig into assertInertiaassert_inertia and the available assertions in detail. First, to assert that the Inertia response has a property, you may use the has method.
1$response->assertInertia(fn (Assert $page) => $page2 // Checking a root-level property...3 ->has('podcasts')45 // Checking nested properties using "dot" notation...6 ->has('podcast.subscribers')7);1$response->assertInertia(fn (Assert $page) => $page2 // Checking a root-level property...3 ->has('podcasts')45 // Checking nested properties using "dot" notation...6 ->has('podcast.subscribers')7);
1response2 .assert_inertia()3 // Checking a root-level property...4 .has("podcasts")5 // Checking nested properties using "dot" notation...6 .has("podcast.subscribers");1response2 .assert_inertia()3 // Checking a root-level property...4 .has("podcasts")5 // Checking nested properties using "dot" notation...6 .has("podcast.subscribers");
To assert that an Inertia property has a specified number of items, provide the expected size as the second argument to hasuse has_count.
1$response->assertInertia(fn (Assert $page) => $page2 // Checking if a root-level property has 7 items...3 ->has('podcasts', 7)45 // Checking nested properties using "dot" notation...6 ->has('podcast.subscribers', 7)7);1$response->assertInertia(fn (Assert $page) => $page2 // Checking if a root-level property has 7 items...3 ->has('podcasts', 7)45 // Checking nested properties using "dot" notation...6 ->has('podcast.subscribers', 7)7);
1response2 .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);1response2 .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);
You may also scope properties (has with a closureusing scope) in order to lessen repetition when asserting against nested properties.
1$response->assertInertia(fn (Assert $page) => $page2 // Creating a single-level property scope...3 ->has('message', fn (Assert $page) => $page4 // We can now continue chaining methods...5 ->has('subject')6 ->has('comments', 5)78 // And can even create a deeper scope using "dot" notation...9 ->has('comments.0', fn (Assert $page) => $page10 ->has('body')11 ->has('files', 1)12 ->has('files.0', fn (Assert $page) => $page13 ->has('url')14 )15 )16 )17);1$response->assertInertia(fn (Assert $page) => $page2 // Creating a single-level property scope...3 ->has('message', fn (Assert $page) => $page4 // We can now continue chaining methods...5 ->has('subject')6 ->has('comments', 5)78 // And can even create a deeper scope using "dot" notation...9 ->has('comments.0', fn (Assert $page) => $page10 ->has('body')11 ->has('files', 1)12 ->has('files.0', fn (Assert $page) => $page13 ->has('url')14 )15 )16 )17);
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});
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.
1$response->assertInertia(fn (Assert $page) => $page2 // Assert that there are 5 comments and automatically scope into the first comment...3 ->has('comments', 5, fn (Assert $page) => $page4 ->has('body')5 // ...6 )7);1$response->assertInertia(fn (Assert $page) => $page2 // Assert that there are 5 comments and automatically scope into the first comment...3 ->has('comments', 5, fn (Assert $page) => $page4 ->has('body')5 // ...6 )7);
1response2 .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 });1response2 .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 });
To assert that an Inertia property has an expected value, use whereequals.
1$response->assertInertia(fn (Assert $page) => $page2 ->has('message', fn (Assert $page) => $page3 // Assert that the subject prop matches the given message...4 ->where('subject', 'This is an example message')56 // Or, assert against deeply nested values...7 ->where('comments.0.files.0.name', 'example-attachment.pdf')8 )9);1$response->assertInertia(fn (Assert $page) => $page2 ->has('message', fn (Assert $page) => $page3 // Assert that the subject prop matches the given message...4 ->where('subject', 'This is an example message')56 // Or, assert against deeply nested values...7 ->where('comments.0.files.0.name', 'example-attachment.pdf')8 )9);
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});
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 etc method exists.
1$response->assertInertia(fn (Assert $page) => $page2 ->has('message', fn (Assert $page) => $page3 ->has('subject')4 ->has('comments')5 ->etc()6 )7);1$response->assertInertia(fn (Assert $page) => $page2 ->has('message', fn (Assert $page) => $page3 ->has('subject')4 ->has('comments')5 ->etc()6 )7);
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});
The missing method is the exact opposite of the has method, ensuring that the property does not exist. This method makes a great companion to the etc method.
1$response->assertInertia(fn (Assert $page) => $page2 ->has('message', fn (Assert $page) => $page3 ->has('subject')4 ->missing('published_at')5 ->etc()6 )7);1$response->assertInertia(fn (Assert $page) => $page2 ->has('message', fn (Assert $page) => $page3 ->has('subject')4 ->missing('published_at')5 ->etc()6 )7);
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});
Testing Partial Reloads
You may use reloadOnly and reloadExceptreload_only and reload_except 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.
1$response->assertInertia(fn (Assert $page) => $page2 ->has('orders')3 ->missing('statuses')4 ->reloadOnly('statuses', fn (Assert $reload) => $reload5 ->missing('orders')6 ->has('statuses', 5)7 )8);1$response->assertInertia(fn (Assert $page) => $page2 ->has('orders')3 ->missing('statuses')4 ->reloadOnly('statuses', fn (Assert $reload) => $reload5 ->missing('orders')6 ->has('statuses', 5)7 )8);
1let page = response.assert_inertia();23page.has("orders").missing("statuses");45page.reload_only(&["statuses"])6 .await7 .missing("orders")8 .has_count("statuses", 5);1let page = response.assert_inertia();23page.has("orders").missing("statuses");45page.reload_only(&["statuses"])6 .await7 .missing("orders")8 .has_count("statuses", 5);
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 loadDeferredPropsload_deferred_props 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.
1$response->assertInertia(fn (Assert $page) => $page2 ->has('users')3 ->has('roles')4 ->missing('permissions') // Deferred prop not in initial response5 ->loadDeferredProps(fn (Assert $reload) => $reload6 ->has('permissions')7 ->where('permissions.0.name', 'edit users')8 )9);1$response->assertInertia(fn (Assert $page) => $page2 ->has('users')3 ->has('roles')4 ->missing('permissions') // Deferred prop not in initial response5 ->loadDeferredProps(fn (Assert $reload) => $reload6 ->has('permissions')7 ->where('permissions.0.name', 'edit users')8 )9);
1let page = response.assert_inertia();23page.has("users")4 .has("roles")5 .missing("permissions"); // Deferred prop not in initial response67page.load_deferred_props()8 .await9 .has("permissions")10 .equals("permissions.0.name", "edit users");1let page = response.assert_inertia();23page.has("users")4 .has("roles")5 .missing("permissions"); // Deferred prop not in initial response67page.load_deferred_props()8 .await9 .has("permissions")10 .equals("permissions.0.name", "edit users");
You may also load specific deferred prop groups by passing the group name to loadDeferredPropscalling load_deferred_groups.
1$response->assertInertia(fn (Assert $page) => $page2 ->has('users')3 ->missing('teams')4 ->missing('projects')5 ->loadDeferredProps('attributes', fn (Assert $reload) => $reload6 ->has('teams', 5)7 ->has('projects')8 ->missing('permissions') // Different group9 )10);1$response->assertInertia(fn (Assert $page) => $page2 ->has('users')3 ->missing('teams')4 ->missing('projects')5 ->loadDeferredProps('attributes', fn (Assert $reload) => $reload6 ->has('teams', 5)7 ->has('projects')8 ->missing('permissions') // Different group9 )10);
1let page = response.assert_inertia();23page.has("users").missing("teams").missing("projects");45page.load_deferred_groups(&["attributes"])6 .await7 .has_count("teams", 5)8 .has("projects")9 .missing("permissions"); // Different group1let page = response.assert_inertia();23page.has("users").missing("teams").missing("projects");45page.load_deferred_groups(&["attributes"])6 .await7 .has_count("teams", 5)8 .has("projects")9 .missing("permissions"); // Different group
You may also load multiple deferred prop groups at once.
1$response->assertInertia(fn (Assert $page) => $page2 ->loadDeferredProps(['default', 'attributes'], fn (Assert $reload) => $reload3 ->has('permissions')4 ->has('teams')5 ->has('projects')6 )7);1$response->assertInertia(fn (Assert $page) => $page2 ->loadDeferredProps(['default', 'attributes'], fn (Assert $reload) => $reload3 ->has('permissions')4 ->has('teams')5 ->has('projects')6 )7);
1response2 .assert_inertia()3 .load_deferred_groups(&["default", "attributes"])4 .await5 .has("permissions")6 .has("teams")7 .has("projects");1response2 .assert_inertia()3 .load_deferred_groups(&["default", "attributes"])4 .await5 .has("permissions")6 .has("teams")7 .has("projects");
Testing Flash Data
You may assert against flash data in your Inertia responses using hasFlash and missingFlashhas_flash, flash, and missing_flash.
1$response->assertInertia(fn (Assert $page) => $page2 // Assert flash data exists...3 ->hasFlash('message')45 // Assert flash data has a specific value...6 ->hasFlash('message', 'Item saved!')78 // Nested values are supported using "dot" notation...9 ->hasFlash('notification.type', 'success')1011 // Assert flash data does not exist...12 ->missingFlash('error')13);1$response->assertInertia(fn (Assert $page) => $page2 // Assert flash data exists...3 ->hasFlash('message')45 // Assert flash data has a specific value...6 ->hasFlash('message', 'Item saved!')78 // Nested values are supported using "dot" notation...9 ->hasFlash('notification.type', 'success')1011 // Assert flash data does not exist...12 ->missingFlash('error')13);
1response2 .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");1response2 .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");
Redirect Responses
The page assertions above only work on rendered Inertia page responses. For redirect responses, you may use assertInertiaFlash and assertInertiaFlashMissing directly on the test response to assert against the session's flash datafollow the redirect with follow_redirects() and assert on the resulting Inertia page.
1$response = $this->post('/users');23$response->assertRedirect('/dashboard')4 ->assertInertiaFlash('message')5 ->assertInertiaFlash('message', 'User created!')6 ->assertInertiaFlash('notification.type', 'success')7 ->assertInertiaFlashMissing('error');1$response = $this->post('/users');23$response->assertRedirect('/dashboard')4 ->assertInertiaFlash('message')5 ->assertInertiaFlash('message', 'User created!')6 ->assertInertiaFlash('notification.type', 'success')7 ->assertInertiaFlashMissing('error');
1app.post("/users")2 .follow_redirects()3 .await4 .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 .await4 .assert_inertia()5 .has_flash("message")6 .flash("message", "User created!")7 .flash("notification.type", "success")8 .missing_flash("error");
Disabling SSR During Tests
Your application may have SSR enabled for development and production, but you might not need it during testing. Set INERTIA_SSR_ENABLED to false in your phpunit.xml.New applications already set ("INERTIA_SSR", "false") in src/testing.rs.
1<env name="INERTIA_SSR_ENABLED" value="false" />1<env name="INERTIA_SSR_ENABLED" value="false" />
1use laravel::foundation::ApplicationBuilder;23pub 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;23pub 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}
You may alternatively use the Inertia::disableSsr() method in your test base class.
1use Inertia\Inertia;23abstract class TestCase extends BaseTestCase4{5 protected function setUp(): void6 {7 parent::setUp();89 Inertia::disableSsr();10 }11}1use Inertia\Inertia;23abstract class TestCase extends BaseTestCase4{5 protected function setUp(): void6 {7 parent::setUp();89 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());34// Evaluated lazily...5Inertia::disableSsr(fn () => app()->runningUnitTests());1// Evaluated immediately...2Inertia::disableSsr(app()->runningUnitTests());34// Evaluated lazily...5Inertia::disableSsr(fn () => app()->runningUnitTests());