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

ignaciodev's avatar

How to test logic inside Inertia::lazy()

On my Controller, I am returning some products using Inertia::lazy() like so:

return Inertia::render('SomePage', [
    'products' => Inertia::lazy(function () {
        // Some logic here to fetch products
    })
]);

What is the approach to test that everything goes well inside the lazy function?

Using $response->assertStatus(200) only tests the stuff outside that function, right?

0 likes
2 replies
ignaciodev's avatar
ignaciodev
OP
Best Answer
Level 2

I found a solution:

$response = $this->withHeaders([
        "x-inertia-partial-component" => [ "SomePage" ],
        "x-inertia" => [ true ],
        "x-inertia-partial-data" => [ 'products' ]
    ])
    ->get('some-route')
;

$response->assertStatus(200);
$this->assertObjectHasAttribute('products', $response->getData()->props);

Please or to participate in this conversation.