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

felixpenrose's avatar

InertiaJS unit test not returning validation errors

I have a similar set up to the post here: https://laracasts.com/discuss/channels/inertia/testing-inertia-with-validation

In that I'm simply trying to test validation errors with Inertia and Laravel. My controller method looks like so;

public function update(UpdateDetailsRequest $request): RedirectResponse
{
    $data = $request->validated();

    return Redirect::route('my_details.show')->with('message', 'Details successfully updated.');
}

And my Form Request

class UpdateDetailsRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'first_name'  => ['required', 'string'],
            'last_name'   => ['required', 'string'],
            // ...
        ];
    }
}

And my pest unit test

test('test validation fails', function () {

    followingRedirects()
        ->patch(route('my_details.update'), ['last_name' => 'Doe'])
        ->assertInertia(
            function (AssertableInertia $page) {
                dd($page);

                return $page->component('MyDetails')
                    ->has('errors.first_name');
            }
        );
});

I've tested using dd() above to print out the values as I get the failure Property [errors.first_name] does not exist. when running the test normally, and the dd confirms the error bag is empty.

-props: array:8 [
    "errors" => []
    //...

If I do this in the browser and inspect using Vue Dev tools I can see the error bag populating correctly when I submit my form, and every example of a test like this I've seen seems to follow this structure so I don't know why mine is not working.

I'm using Inertias useForm() helper to handle form submission on the front end.

const form = useForm({
    first_name: details.first_name,
    last_name: details.last_name,
});

const form_submit = (form, route) => {
    form.patch(route, { /**/ });
}

<form @submit.prevent="form_submit(form, route('my_details.update'))">

Anything obvious I'm missing here?

0 likes
6 replies
gych's avatar
gych
Best Answer
Level 29

I personally use assertInvalid to check for validation errors

For your use case that should work like this:

   followingRedirects()
        ->patch(route('my_details.update'), ['last_name' => 'Doe'])
        ->assertInvalid(['first_name']);
1 like
felixpenrose's avatar

That certainly does work to test the validation error specifically (though I do have to remove followingRedirects() from the chain). If I remove it and retain assertInertia... then my error turns to Not a valid Inertia response

I was hoping to add some more to this test and make it Intertia specific, it just seems that the redirection when run via unit tests seems to remove the errors.

For not though at least utilising assertInvalid will help, thank you.

gych's avatar

@felixpenrose No problem :) What were you planning to add to this test ? Its better to test only one request per test and don't use for example multiple Inertia request within the same test.

felixpenrose's avatar

@gych I've not worked out everything I wish to test yet, but something along the lines of testing other details sent back in the Inertia response. This is the first "failure" test I've tried with Inertia so it might be that I don't really need much more in reality.

I've been able to test the Inertia response on other tests where the route is a basic Route::get() with an Inertia::render() method in the controller. I guess I'm hoping this won't impact future tests if I can't get something that uses a redirect where I'd like to check the errors.

Using the Inertia helper is a bit more specific so it feels like a more robust test.

gych's avatar

@felixpenrose Personally I always use assertInvalid to test the validation in my Inertia projects. I like the shorter syntax.

When checking other details from the response you're propably testing more than only the validation. In that case its better to write another test for that and not test that within the same validation fails test.

Please also don't forget to close your thread by selecting the best answer. If you have more questions don't hesitate to reach out :)

Please or to participate in this conversation.