Not a valid Inertia response
Asked Answered
C

4

5

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()
        ));
}
Cambodia answered 5/7, 2022 at 13:1 Comment(3)
Does it work if you use Assert instead of AssertableInertia? The older documentation uses Assert. The documentation on testing on the website (inertiajs.com/testing) is very light and suggests that you should follow the old documentation for now.Kantos
yes it does not work, i don't know what i do wrong 2Cambodia
I am not sure, but others have reported this issue too. Take a look at github.com/inertiajs/inertia-laravel/issues/292 and see if there's a 3rd party package interfering with tests.Kantos
G
8

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. 🙏

Gereron answered 22/8, 2022 at 12:30 Comment(0)
M
1

Or if you're using Vite

$this->withoutVite();
Microsurgery answered 22/6, 2023 at 8:44 Comment(0)
B
1

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('/')
Bitternut answered 10/9 at 10:17 Comment(0)
B
0

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('/');
Beebeebe answered 10/9 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.