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

dsampaolo's avatar

Pest / Dusk / MustVerifyEmail

Hi,

I have a situation. I try to run a simple Dusk test using Pest, to ensure that my Login page redirects to his Dashboard when a user is already logged in. When I run the test multiple times, it sometime passes and sometimes fails, without modifying the code.

Here is my test :

test('LoginPage redirects a logged in user to his dashboard', function () {
    $this->browse(function (Browser $browser) {
        $browser->loginAs(User::factory()->create())
            ->visitRoute('login')
            ->assertUrlIs(route('dashboard'));
    });
});

It uses the User factory, which is as follow :

  public function definition()
    {
        return [
            'email' => $this->faker->unique()->safeEmail(),
            'email_verified_at' => now()->subWeek()->subHour()->subMinutes(5),
            'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }

Instead of redirecting to the Dashboard, the test sometimes redirects the user to /verify-email. I double-checked that user.email_verified_at is not null and is a past date, everything seems to be OK.

If I remove the MustVerifyEmail contract, the test passes everytime.

Any idea would be appreciated.

0 likes
0 replies

Please or to participate in this conversation.