pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.62k stars 358 forks source link

PEST with Laravel Modules and Laravel Livewire plugin #382

Closed dcblogdev closed 3 years ago

dcblogdev commented 3 years ago

I would really like to use PEST with Laravel Modules package. (https://github.com/nWidart/laravel-modules)

PEST runs with modules by itself but where the problem lies is the plugins for example I have a fresh Laravel install with a contacts module which has this test file

ContactTest.php

<?php

use App\Http\Livewire\Counter;
use function Pest\Livewire\livewire;

it('can be incremented', function () {
    livewire(Counter::class)
        ->call('increment')
        ->assertSee(1);
});

When I run the test I get:

Call to undefined method PHPUnit\Framework\TestCase::livewire()

I have these installed:

"pestphp/pest": "^1.16",
"pestphp/pest-plugin-faker": "^1.0",
"pestphp/pest-plugin-global-assertions": "^1.0",
"pestphp/pest-plugin-laravel": "^1.1",
"pestphp/pest-plugin-livewire": "^1.0",

I've uploaded a demo project to show this.

https://github.com/dcblogdev/laravel-module-pest-demo/blob/master/Modules/Contacts/Tests/Feature/ContactTest.php

Are there any steps I need to complete?

brunolipe-a commented 3 years ago

I managed to make it work coping the Pest.php file to de Modules folder. And to run the tests I ran in the console ./vendor/bin/pest --test-directory Modules. I think that will be better if we can run all tests, both in tests and Modules folders.

Pest.php inside Modules:

uses(Tests\TestCase::class)->in('');
dcblogdev commented 3 years ago

Thanks you! this test now runs:

use App\Http\Livewire\Counter;
use function Pest\Livewire\livewire;

uses(Tests\TestCase::class);

it('can be incremented', function () {
    livewire(Counter::class)
        ->call('increment')
        ->assertSee(1);
});

As you mentioned running the test runner with options runs ./vendor/bin/pest --test-directory Modules

It would be nice it the normal ./vendor/bin/pest worked but this workaround works for me.

In PhpStorm the default ./vendor/bin/pest test runner is used to add the options edit the test configuration:

edit configuration

To have all tests configurations have this out the box add --test-directory Modules to the test runner options in the configuration template:

edit configuration template
dcblogdev commented 3 years ago

Running ./vendor/bin/pest --test-directory Modules --parallel returns the original error

Call to undefined method Tests\TestCase::livewire()

But at least I can run the tests without using parallel for now.

nunomaduro commented 3 years ago

No plans to investigate this issue. Fell free to submit a pull request that addresses this issue.

garbinmarcelo commented 2 years ago

Hi @dcblogdev , are you still using PESTPHP with Laravel Modules? I currently use Laravel Modules and am learning PESTPHP.

Could you tell us a little about your experience so far?

I'm thinking about getting started using Inertia.js in my projects too, so I'm still studying about that.

Thanks

dcblogdev commented 2 years ago

@garbinmarcelo pest works fine with modules now.

most of the time you only need to import:

uses(Tests\TestCase::class);

for example:

<?php

use Modules\Contacts\Models\Contact;

uses(Tests\TestCase::class);

test('can see contact list', function() {
    $this->authenticate();
   $this->get(route('app.contacts.index'))->assertOk();
});
KuzminDima commented 2 years ago

It seems to me that the best option is to specify the full path to the module Example:

// Pest.php
uses(Tests\TestCase::class)->in(
    'Feature',
    __DIR__ . '/../Modules/TestModule/Tests/Feature',
);

uses(Illuminate\Foundation\Testing\RefreshDatabase::class)->in(
    'Feature',
    __DIR__ . '/../Modules/TestModule/Tests/Feature',
);
andresilvagomez commented 9 months ago

@dcblogdev did you have some update with parallel testing?

It's working fine for me, but I need coverage in parallel mode

back2Lobby commented 2 months ago

For me, nothing worked except following what's mentioned in the documentation lol.

I tried the method specified above like this but it didn't work:

uses(
    DuskTestCase::class,
    DatabaseMigrations::class
)->in(__DIR__ . '/../Modules/TestModule/Tests/Feature');

But adding the module pattern like this worked like a charm:

uses(
    DuskTestCase::class,
    DatabaseMigrations::class
)->in('../Modules/*/Tests/Feature');

Plus we don't need to mention it for every single module 🥳.