F
Filamentβ€’9mo ago
Adam Holmes

Writing PEST tests for Filament Tables

Hi, I have a ListItemResource which is simply to store a key value pair in the database. I have a ListListItems.php which includes the table of all the entries. I'm trying to write a test to ensure that the table is populated. So far I have the following tests: Render pages - Works fine βœ… Check "value" column is in table - Works fine βœ… Check all items in table - Fails ❌ I've tried all sorts of different variations including PHPUnit and PEST. I currently have PEST installed along with the livewire plugin too. The user is authenticated via actingAs method. All namespaces etc are used. It seems that the tests just blurts out the HTML to the terminal, but can never match any results from the array of ListItems. The code is as close to the docs here as I can get: https://filamentphp.com/docs/3.x/panels/testing#table but still no joy. I've attached a screenshot showing the test code and the error in the terminal. Thanks in advance πŸ™‚
No description
Solution:
Are you modifying something in the query? It is weird because your table has 12 records.. try ->assertCountTableRecords(12)...
Jump to solution
8 Replies
LeandroFerreira
LeandroFerreiraβ€’9mo ago
dd(ListItem::count()) What is the output?
Adam Holmes
Adam HolmesOPβ€’9mo ago
10 // tests/Feature/ListItemTest.php:32 10 - looks like they're being created
LeandroFerreira
LeandroFerreiraβ€’9mo ago
where did you put it?
Adam Holmes
Adam HolmesOPβ€’9mo ago
Right after the create:
it('check all lists items are shown', function () {
$list_items = ListItem::factory()->count(10)->create();
dd(ListItem::count());

livewire(ListItemResource\Pages\ListListItems::class)
->assertCanSeeTableRecords($list_items)
->assertCountTableRecords(10);
});
it('check all lists items are shown', function () {
$list_items = ListItem::factory()->count(10)->create();
dd(ListItem::count());

livewire(ListItemResource\Pages\ListListItems::class)
->assertCanSeeTableRecords($list_items)
->assertCountTableRecords(10);
});
Solution
LeandroFerreira
LeandroFerreiraβ€’9mo ago
Are you modifying something in the query? It is weird because your table has 12 records.. try ->assertCountTableRecords(12)
Adam Holmes
Adam HolmesOPβ€’9mo ago
So I simplified the test so that it has just one assert - the assertCountTableRecords on. And it's coming back as Failed asserting that 1 is identical to 10. Now you mention modifying the query, I set some default filters on the, ListItemResource so I suspect that will be having an impact. Aha - removing the default filters made this test pass and also the previous assertCanSeeTableRecords
@php artisan test
PASS Tests\Unit\ExampleTest βœ“ that true is true 0.01s
PASS Tests\Feature\DashboardTest βœ“ can render pages 18.80s
PASS Tests\Feature\ListItemTest βœ“ can render pages 1.09s
βœ“ can see value columns on table 1.04s
βœ“ it check all lists items are shown 1.03s
Tests: 5 passed (28 assertions) Duration: 22.10s
LeandroFerreira
LeandroFerreiraβ€’9mo ago
yep, the test is right, the count is different actually
Adam Holmes
Adam HolmesOPβ€’9mo ago
Thanks for your help πŸ™‚

Did you find this page helpful?