© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•7mo ago•
3 replies
Kovah

How to test Filament widgets?

I am a bit lost right now. I try to test a Filament (v3) widget via Pest/PhpUnit, but can't just get it to work. The stats widget itself is nothing special, the only code is in the getStats() method which returns a few Stat components.

The getStats() method is protected by default, so I would need a reflection class to access that data, which feels wrong.
My second idea was to just render the HTML, but that leads to an error.

test('Costs for upcoming domain renewals are calculated correctly', function () {
    Domain::factory()->create([
        'next_renewal' => now()->addMonth(),
        'price' => 10,
        'status' => DomainStatus::Active->value,
    ]);

    $html = (new DomainCostsWidget)->render();
    $this->assertStringContainsString('10', $html);
});
test('Costs for upcoming domain renewals are calculated correctly', function () {
    Domain::factory()->create([
        'next_renewal' => now()->addMonth(),
        'price' => 10,
        'status' => DomainStatus::Active->value,
    ]);

    $html = (new DomainCostsWidget)->render();
    $this->assertStringContainsString('10', $html);
});

Illuminate\View\ViewException: Using $this when not in object context
Illuminate\View\ViewException: Using $this when not in object context


Unfortunately, there is no official documentation on that, and after looking through the base widget classes, it seems that there is no way to properly get the data without initializing it first, which seems to happen only internally.

Has anyone solved this? Can't imagine that people just create widgets without testing what they return. Also, moving the business logic into a separate class feels equally wrong.
Solution
I think they are livewire components. Did you try testing them via the Livewire helper?
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

How to write test for chart widgets or do we need to test chart widgets
FilamentFFilament / ❓┊help
2y ago
Cant install Filament widgets
FilamentFFilament / ❓┊help
3y ago
Filament Table Widgets with action
FilamentFFilament / ❓┊help
9mo ago
Filament v3 dinamic widgets cards
FilamentFFilament / ❓┊help
3y ago