Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Swaz's avatar
Level 20

How do you test Inertia.js?

I know there used to be an Inertia testing packing, but it has since been merged into Laravel as the new json testing helpers.

But I don't understand how I would test a simple controller method like this:

public function show(Client $client)
{
    return Inertia::render('Clients/Show', [
        'client' => [
            'id' => $client->id,
            'name' => $client->name,
            'email' => $client->email,
        ],
    ]);
}

Here's what I have but I keep getting the error: Invalid JSON was returned from the route.

/** @test */
public function show()
{
    $client = Client::factory()->create([
        'name' => 'Apple',
        'email' => '[email protected]',
    ]);

    $this->get(route('clients.show', $client))
        ->assertJson(fn (AssertableJson $json) => 
            $json->has('client')
                 ->where('name', 'Apple')
                 ->where('email', '[email protected]')
        );
}
0 likes
6 replies
Swaz's avatar
Level 20

If anyone else stumbles across this, here's an example of how you might test the controller method I posted above.

namespace Tests\Feature\Http\Controllers;

use App\Models\Client;
use Inertia\Testing\Assert;
use Tests\TestCase;

class ClientControllerTest extends TestCase
{
    /** @test */
    public function show()
    {
        $client = Client::factory()->create([
            'name' => 'Apple',
            'email' => '[email protected]',
        ]);

        $this->get(route('clients.show', $client))
            ->assertInertia(fn (Assert $page) => $page
                ->component('Clients/Show')
                ->has('client', fn (Assert $page) => $page
                    ->where('id', $client->id)
                    ->where('name', 'Apple')
                    ->where('email', '[email protected]')
                )
            );
    }
}
34ML's avatar

@Swaz please sir could you help me

i run this test function

public function test_home_page_sponsors() : void
    {
        $this
            ->get('/')
            ->assertInertia(fn (Assert $page) => $page
            ->component('HomePage', false));
    }

but every time i got stuck with this error

Not a valid Inertia response.

could you please help me

Swaz's avatar
Level 20

@34ML Are you returning an Inertia response from the controller?

public function index()
{
    return Inertia::render('Homepage');
}

Please or to participate in this conversation.