Testing Inertia Applications
jengo/inertia provides seamless testing utilities that integrate cleanly with CodeIgniter 4's FeatureTestTrait and Pest PHP without requiring awkward trait method aliasing or custom request verb overrides.
1. Setting Up Test Cases
In your Pest test or PHPUnit test case, simply use CodeIgniter's FeatureTestTrait and Jengo's InertiaAssertions trait:
php
namespace Tests\Feature;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\FeatureTestTrait;
use Jengo\Inertia\Testing\InertiaAssertions;
class DashboardTest extends CIUnitTestCase
{
use FeatureTestTrait;
use InertiaAssertions;
}Or when using Pest:
php
use Tests\Support\FeatureRequestTestCase;
uses(FeatureRequestTestCase::class);
test('dashboard renders user data', function () {
$result = $this->get('/dashboard');
$this->assertInertia($result, function ($page) {
$page->component('Dashboard')
->has('auth.user')
->where('auth.user.name', 'Ian')
->etc();
});
});2. Fluent Inertia Assertions (assertInertia)
The assertInertia() assertion verifies that the HTTP response was generated by Inertia and yields a fluent AssertableInertia object to test components, props, and reload behaviors.
php
$result = $this->get('/orders');
$this->assertInertia($result, function (AssertableInertia $page) {
$page->component('Orders/Index')
->has('orders', 10)
->where('orders.0.status', 'completed')
->missing('secret_token')
->etc();
});3. The inertia_test() / inertia() Helper
If you prefer fluent chaining directly on the response object:
php
$result = $this->get('/dashboard');
$this->inertia($result)
->assertInertiaFlash('message', 'Profile updated')
->assertInertiaFlashMissing('error');You can also inspect props directly using dot-notation:
php
$props = $this->inertia($result)->inertiaProps();
$name = $this->inertia($result)->inertiaProps('user.name');