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

falconsmilie's avatar

Livewire Unit Test Flash Data Assertion

Hi.

There's something with unit testing flash data that I'm not understanding.

Livewire component has this code; session()->flash('errorMessage', 'blah blah')

Unit test has this code;

$response = Livewire::test(Login::class)
            ->set('email', '[email protected]')
            ->set('password', 'wrongPassword')
            ->call('submit')
//            ->assertSessionHas('_flash.new.0');
            ->assertSessionHasInput('errorMessage', 'blah blah');

The assertSessionHas() call works, but only when i drill down through the array (like in the commented example above).

assertSessionHasInput('errorMessage) or assertSessionHas('errorMessage') does not work.

The result of $response->dumpSession(); is;

array:1 [
  "_flash" => array:2 [
    "new" => array:1 [
      0 => "errorMessage"
    ]
    "old" => []
  ]
]

I'm referencing the docs here; https://laravel.com/docs/8.x/http-tests#assert-session-has-input

Please help me understand why checking for the errorMessage key is failing.

0 likes
4 replies
docTRIN's avatar

I am having the same issue. I have a livewire action that sends flash message to the session

session()->flash('success', 'Email sent');

The message works in browser testing, but non in my PHPUnit testing. The result of $response->dumpSession(); is;

array:1 [
  "_flash" => array:2 [
    "new" => array:1 [
      0 => "success"
    ]
    "old" => []
  ]
1 like
pgferro's avatar

bumping to check if somebody came up with a solution / workaround ..

1 like
zoker's avatar

If you are using PEST, you can extend expect (in pest.php) with:

expect()->extend('assertFlashMessageHas', function ($key) {
    return $this->assertSessionHas('_flash', function ($session) use ($key) {
        return in_array($key, $session['new']);
    });
});

and using:

$response = Livewire::test(\App\Livewire\SomeClass::class)
        ->call('method');

expect($response)->assertFlashMessageHas('forgot-password-success');

It may not be as pretty, but it works.

2 likes

Please or to participate in this conversation.