Problems testing with assertHasErrors

I’m trying to test validations on CreateTeamForm component of Jetstream but assertHasErrors doesn’t catch the error (even if the component is catching it correctly) and always return a “Component has no errors.” message.

It seems that it loose the error bag on the way (maybe on the toSubsequentResponse method of Response class but i’m not sure)

Here is the test, you can launch it on a laravel jetstream installation.

   public function name_is_required()
{
    $this->actingAs(User::factory()->create());

    Livewire::test(CreateTeamForm::class)
        ->set('state.name', '')
        ->call('createTeam')
        ->assertHasErrors(['name' => 'required']);
}

I’m using livewire 2.2.5 on laravel 8.5

Any tips?

I believe I’m experiencing the same – or a similar – problem.

From everything I can see, the code works as expected when I test it manually…that is, when I serve up the app, go to the livewire component, enter invalid data, and see that livewire recognizes the invalid field and accurately reports it.

But…when I run the test, it doesn’t recognize the error.

My test is almost identical to yours and is even called the same. It’s testing that the name field is required. I’m not sure what to do.

I’m using livewire 2.2.9 and laravel 8.9.0.

Hi! I think i’ve found the problem…
Livewire::assertHasErrors() tests only work using Livewire validate and not the general Laravel validation…
in my case using $this->validate() inside the component fixes the problem

@LucaRossi Thx for the response. I’m changing from using Validator::make() to $this->validate()…but having some problems…

First, I have two forms, a create and an edit…so I was using Validator::make()->validateWithBag(). It’s not immediately clear how to handle this via $this->validate().

Second, it’s not clear how the validation code knows which data to validate. Using Validator::make(), I explicitly pass this in.

I’m going to re-read the docs, check out @calebporzio’s videos, and maybe ask Uncle Google if nothing falls out.

Thx again…! :nerd_face: