vincent15000's avatar

Testing with InertiaJS - Not a valid Inertia response

Hello,

When I write exactly the same test as in the documentation, I get this error.

Not a valid Inertia response

describe('CategoryController', function () {
    describe('index', function () {
        it('returns inertia response with categories for authorized user', function () {
            $user = User::factory()->hasAttached(Role::factory()->hasAttached(Permission::factory()->state(function () {
                return ['name' => 'view all categories'];
            })))->create();
            $categories = Category::factory()->count(3)->create();
            
            $response = $this
                ->actingAs($user)
                ->get(route('admin.categories.index'))
                ->assertInertia(fn (AssertableInertia $page) => $page
                    // ->component('Admin/Categories/Index')
                    ->has('categories.data', 3)
                    // ->has('categories.data.0', fn (AssertableInertia $page) => $page
                    //     ->has('id')
                    //     ->has('name')
                    //     ->etc()
                    // )
                );

            dump($response);
        });
    });
});

I have searched on the web without any relevant information.

Any idea ?

Thanks for your help.

V

0 likes
2 replies
vincent15000's avatar

So the problem came from my AuthServiceProvider (my own provider, not the one from Laravel) where I load gates with Gate::define().

In the test, the gate definition via the provider doesn't work ... why ? I must reload the gates directly inside the test and then it works.

But now I have another problem.

$response = $this->actingAs($user, 'web')
    ->get(route('admin.categories.index'))
    ->assertInertia(fn (AssertableInertia $page) =>
        $page->component('Admin/Categories/Index')
            ->has('categories.data', 3)
    );

If I execute this assertion, it doesn't work => Admin/Categories/Index not found.

If I change Admin/Categories/Index for Categories/Index => Categories/Index isn't the same as expected Admin/Categories/Index.

Hmmm ... strange ...

Any idea ?

vincent15000's avatar
vincent15000
OP
Best Answer
Level 63

I have the solution.

I have renamed Pages to pages (lowercase).

I had to publish the InertiaJS configuration file and rename Pages to pages in the configuration file.

Please or to participate in this conversation.