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

Bihacsy's avatar

Inertia modular test

I would like some help with the tests. I wrote a macro that switches the components modularly, e.g. Inertia:module("admin:index"). how can I modify the AssertableInertia class to match this component definition. I have already created the AssertableInertiaModular class, where the extends element is AssertableInertia, into which I will add the componet class.

public function component(string $value = null, $shouldExist = null): self{

    PHPUnit::assertSame($value, $this->component, 'Unexpected Inertia page component.');

    if ($shouldExist || (is_null($shouldExist) && config('inertia.testing.ensure_pages_exist', true))) {
        try {
            $parts = explode('::', $value);
            if (count($parts) > 1) {
                // The component name is modular
                $module = $parts[0];
                $component = $parts[1];
                app('inertia.testing.view-finder')->find($module . '/' . $component);
            }
            else {
                app('inertia.testing.view-finder')->find($value);
            }
        } catch (InvalidArgumentException $exception) {
            PHPUnit::fail(sprintf('Inertia page component file [%s] does not exist.', $value));
        }
    }
    return $this;
}

after i did that i ran a test and got the following error.

  1. Modules\Admin\tests\Unit\AdminIndexTest::testExample TypeError: Modules\Admin\tests\Unit\AdminIndexTest::Modules\Admin\tests\Unit{closure}(): Argument #1 ($page) must be of type Modules\Main\Testing\AssertableInertiaModular, Inertia\Testing\AssertableInertia given, called in D:\Project\Core\vendor\inertiajs\inertia-laravel\src\Testing\TestResponseMacros.php on line 18

here is the test file I made, don't stone me. I'm just learning this now.

public function testExample() { $response=$this->get('/admin'); $response->assertInertia(function (Assert $page) { $page->component('Admin::index'); }); } I modified the nwidart module packet to be able to build a modular site. Can someone help me solve the problem or give me an idea where I should start?

0 likes
1 reply
LaryAI's avatar
Level 58

To solve the issue you're encountering, you need to ensure that your custom AssertableInertiaModular class is being used instead of the default AssertableInertia class. The error you're seeing indicates that the assertInertia method is still using the default AssertableInertia class.

Here's a step-by-step solution to address this:

  1. Create a Custom Test Response Macro: You need to override the assertInertia macro to use your custom AssertableInertiaModular class.

  2. Register the Custom Macro: Ensure that your custom macro is registered in your test setup.

  3. Modify Your Test: Use the custom macro in your test.

Step 1: Create a Custom Test Response Macro

First, create a custom macro that uses your AssertableInertiaModular class. You can place this in a service provider or a dedicated test setup file.

use Illuminate\Testing\TestResponse;
use Modules\Main\Testing\AssertableInertiaModular;

TestResponse::macro('assertInertiaModular', function (Closure $callback) {
    $assert = new AssertableInertiaModular($this);

    $callback($assert);

    return $this;
});

Step 2: Register the Custom Macro

Ensure that this macro is registered before your tests run. You can do this in a service provider or in the setUp method of your test case.

For example, if you want to register it in a service provider:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Testing\TestResponse;

class TestServiceProvider extends ServiceProvider
{
    public function boot()
    {
        TestResponse::macro('assertInertiaModular', function (Closure $callback) {
            $assert = new AssertableInertiaModular($this);

            $callback($assert);

            return $this;
        });
    }
}

Make sure to register this service provider in your config/app.php under the providers array.

Step 3: Modify Your Test

Now, modify your test to use the new assertInertiaModular macro.

public function testExample()
{
    $response = $this->get('/admin');
    $response->assertInertiaModular(function (AssertableInertiaModular $page) {
        $page->component('Admin::index');
    });
}

Full Example

Here is the full example with all the pieces put together:

Custom Macro Registration (e.g., in a service provider)

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Testing\TestResponse;
use Modules\Main\Testing\AssertableInertiaModular;

class TestServiceProvider extends ServiceProvider
{
    public function boot()
    {
        TestResponse::macro('assertInertiaModular', function (Closure $callback) {
            $assert = new AssertableInertiaModular($this);

            $callback($assert);

            return $this;
        });
    }
}

Custom AssertableInertiaModular Class

Ensure your AssertableInertiaModular class is correctly defined and extends AssertableInertia.

namespace Modules\Main\Testing;

use Inertia\Testing\AssertableInertia;

class AssertableInertiaModular extends AssertableInertia
{
    public function component(string $value = null, $shouldExist = null): self
    {
        PHPUnit::assertSame($value, $this->component, 'Unexpected Inertia page component.');

        if ($shouldExist || (is_null($shouldExist) && config('inertia.testing.ensure_pages_exist', true))) {
            try {
                $parts = explode('::', $value);
                if (count($parts) > 1) {
                    // The component name is modular
                    $module = $parts[0];
                    $component = $parts[1];
                    app('inertia.testing.view-finder')->find($module . '/' . $component);
                } else {
                    app('inertia.testing.view-finder')->find($value);
                }
            } catch (InvalidArgumentException $exception) {
                PHPUnit::fail(sprintf('Inertia page component file [%s] does not exist.', $value));
            }
        }
        return $this;
    }
}

Test Case

public function testExample()
{
    $response = $this->get('/admin');
    $response->assertInertiaModular(function (AssertableInertiaModular $page) {
        $page->component('Admin::index');
    });
}

By following these steps, you should be able to use your custom AssertableInertiaModular class in your tests without encountering the type error.

Please or to participate in this conversation.