Filamentphp testing with Pest Call to undefined function Pest\Livewire\livewire()

Filamentphp testing with Pest Call to undefined function Pest\Livewire\livewire()

Posted on:December 29, 2023 at 01:00 PM

Currently I am using laravel, filamentphp, and pest for testing. I went through the filament testing documentation, but when I try to run the following testing:

use function Pest\Livewire\livewire;
 
it('can list posts', function () {
    $posts = Post::factory()->count(10)->create();
 
    livewire(PostResource\Pages\ListPosts::class)
        ->assertCanSeeTableRecords($posts);
});

I get this error Call to undefined function Pest\Livewire\livewire().

More details:

 it can list scholarships                                                                                                                                                                                                        0.13s  
  ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────  
   FAILED  Tests\Feature\Filament\Resources\ScholarshipResourceTest > it can list scholarships                                                                                                                                     Error   
  Call to undefined function Pest\Livewire\livewire()

  at tests/Feature/Filament/Resources/ScholarshipResourceTest.php:24
     20▕ 
     21▕ it('can list scholarships', function () {
     22▕     $scholarships = Scholarship::factory(10)->create();
     23▕ 
  24     livewire(ListScholarships::class)->assertCanSeeTableRecorded($scholarships);
     25▕ });
     26▕ 

  1   tests/Feature/Filament/Resources/ScholarshipResourceTest.php:24

Mainly it’s because of your project is missing a package that didn’t mentioned in the documentation unfortunately 😔.

How to solve it?

You need to install this package:

composer require pestphp/pest-plugin-livewire --dev

Now, your editor e.g. VS Code should able to detect this:

use function Pest\Livewire\livewire;

BTW, if you have still issue, maybe use composer dump-autoload.

Thanks.