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

ismailalterweb's avatar

Intermittent 403 on FilamentPHP + Pest test:

Hello everyone,

I'm facing an intermittent issue with a Pest test in a FilamentPHP project. The test sometimes passes and sometimes fails, without any code changes between runs. Here's the situation:

The test:

it('can render index page', function (): void {
    asAdmin();
    $this->get(UserResource::getUrl())
        ->assertSuccessful();
});

✅ Sometimes:

PASS  Tests\Feature\Filament\UserResource\ListUsersTest
✓ it can render index page

❌ Other times:

FAILED  Tests\Feature\Filament\UserResource\ListUsersTest > it can render index page  
Expected response status code [>=200, <300] but received 403.
Failed asserting that false is true.

at tests\Feature\Filament\UserResource\ListUsersTest.php:14

Helper method on Pest.php:

function asAdmin(): TestCase
{
    $user = User::factory()->create([
        'role' => Role::Admin->value,
    ]);

    return test()->actingAs($user);
}

UserPolicy:

public function viewAny(User $user): bool
{
    return $user->role === Role::Admin;
}

All other policy methods check for the same condition: role === Role::Admin.

User model casts:

protected function casts(): array
{
    return [
        'role' => Role::class,
        ...
    ];
}

What I’ve tried:

  • Confirmed the enum cast works fine.
  • Confirmed the role is being set correctly.
  • Inspected the response with dump() when it fails, it shows the default Laravel 403 HTML page.
  • Cleared caches, config, route, view, etc.

What could be causing this random behavior?

It’s like sometimes the policy check is skipped or misinterpreted, possibly due to request context, timing, or test isolation? Could it be related to session state, middleware, or how Filament bootstraps resources during tests?

Has anyone experienced something similar with Pest + Filament? Would appreciate any leads or debugging strategies 🙏

0 likes
3 replies
Glukinho's avatar

Does it happen if you try to fetch a page with browser, not when testing?

You can put into AppServiceProvider ->boot() method:

Gate::after(function (User $user, string $ability, bool|null $result, mixed $arguments) {
	Log::debug('auth info', compact('user', 'ability', 'result', 'arguments'));
});

You should see authorization logs in laravel.log, you can debug the problem there.

jaseofspades88's avatar

In my experience, flaky tests like this have usually boiled down to something in one of the factories that simply is different from one test to the next. A type, a flag or something similar. Do you have a policy or gate checking and authorising your routes?

Please or to participate in this conversation.