ibrahim khalaf
ibrahim khalaf

Reputation: 81

Not a valid Inertia response

I am using inertiajs/inertia-laravel 0.6.3 with Laravel 8.40.

I get this response every time I run my test, and I have checked everything. Sometimes, I use Assert instead of AssertableInertia.

If someone could point me in the right direction, I would appreciate it.

use Inertia\Testing\AssertableInertia;
use RefreshDatabase;

public function test_home_page_sponsors(): void
{
    $sponsors = Sponsor::factory()->count(5)->create();

    $this->get('/')
        ->assertInertia(fn (AssertableInertia $page) => $page
        ->component('HomePage')
        ->has('sponsors.data', 5)
        ->has(
            'sponsors.data.0',
            fn (AssertableInertia $page) => $page
                ->where('title', $sponsors[0]->title)
                ->etc()
        ));
}

Upvotes: 5

Views: 2666

Answers (4)

rozsazoltan
rozsazoltan

Reputation: 7649

I was surprised to find that the responses here do not contain references to official documentation.

Disable build tools

In Laravel tests you can disable the automatic inclusion of the asset bundler that Laravel uses to compile frontend assets (JavaScript, CSS, etc.). These methods are useful when you are testing your application and want to prevent Laravel from trying to load unnecessary frontend-related files.

Vite (supported Laravel 9.x or above)

Starting from version 9.x, Laravel officially recommends using it paired with Vite. Therefore, the withoutVite command has replaced the previous withoutMix command.

If you would prefer to mock Vite during testing, you may call the withoutVite method, which is available for any tests that extend Laravel's TestCase class:

test('without vite example', function () {
   $this->withoutVite();

   // ...
});

Webpack (supported from Laravel 6.x to 8.x)

From Laravel 9.x need migrating back for Webpack using, but by default, with the higher versions, you need the guides related to Vite.

If you would prefer to mock Webpack during testing, you may call the withoutMix method, which is available for any tests that extend Laravel's TestCase class:

test('without webpack example', function () {
   $this->withoutMix();

   // ...
});


Disable Laravel Functions

I only highlighted one thing from the documentation (HTTP tests), but there is much more in it that we can disable depending on the situation.

Exception Handling

You may totally disable exception handling for a given request by invoking the withoutExceptionHandling method before making your request:

$response = $this->withoutExceptionHandling()->get('/');

Upvotes: 0

Nicolas Schubhan
Nicolas Schubhan

Reputation: 1756

You may also need withoutMiddleware if you need Auth in your tested page

$this->withoutVite();
$this->withoutMiddleware();

And code should be added before to call

$this->get('/')

Upvotes: 1

Jack Kitley
Jack Kitley

Reputation: 410

Or if you're using Vite

$this->withoutVite();

Upvotes: 1

Arno Stalpaert
Arno Stalpaert

Reputation: 91

I had a similar issue in my own Laravel project today. Do you have (Webpack) versioning enabled for you assets?

If this is the case, the test is failing because it cannot find the Mix manifest file. You should tell the test to ignore the Mix versioning:

$this->withoutMix();

I discovered the Mix error message by debugging the test:

$this->withoutExceptionHandling();

Hopefully this fix might work for you aswell. 🙏

Upvotes: 8

Related Questions