Reputation: 81
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
Reputation: 7649
I was surprised to find that the responses here do not contain references to official documentation.
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.
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'sTestCase
class:test('without vite example', function () { $this->withoutVite(); // ... });
withoutVite()
- Laravel 9.x APIFrom 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'sTestCase
class:test('without webpack example', function () { $this->withoutMix(); // ... });
withoutMix()
- Laravel 8.x APII only highlighted one thing from the documentation (HTTP tests), but there is much more in it that we can disable depending on the situation.
You may totally disable exception handling for a given request by invoking the
withoutExceptionHandling
method before making your request:$response = $this->withoutExceptionHandling()->get('/');
withoutExceptionHandling()
- Laravel 11.x APIUpvotes: 0
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
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