InterNACHI / modular

Modularize your Laravel application
MIT License
753 stars 60 forks source link

Has anyone tried using Pest with this package ? #52

Open vishytk opened 1 year ago

vishytk commented 1 year ago

I am trying this package to modularise one of our application. I have configured Laravel to use pest for testing.

In the main Laravel tree, I have tests/Pest.php configuration file.

I am creating modules in modules directory instead of app-modules. The namespace is set to Talkad.

I have created a module called hms-post.

When I run pest, I see

Modules\hmspost\tests\Unit\ExampleTest instead of Talkad\HmsPost\Tests\Unit\ExampleTest

The ExampleTest (modules/hms-post/tests/Unit/ExampleTest.php)

<?php

namespace Talkad\HmsPost\Tests\Unit;

use Tests\TestCase;

uses(TestCase::class);

test('that true is true', function () {
    echo get_class($this); // shows T\Modules\hmspost\tests\Unit\ExampleTest
    expect(true)->toBeTrue();
});

The Pest configuration

tests/Pest.php under the main Laravel tree

<?php

use Tests\TestCase;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;

uses(
    TestCase::class,
    LazilyRefreshDatabase::class,
)->in(__DIR__);

Need help on this.

inxilpro commented 1 year ago

I don't use pest much yet, but I know @DanielCoulbourne does. Daniel—have you used modular in Pest codebases?

My guess is that you need to update Pest.php to include the modules directory, but I'm not 100% sure…

dcblogdev commented 1 year ago

I've used Pest with Modular, I set the modules path in phpunit.xml

<testsuite name="Modules">
  <directory>app-modules/*/Tests/Feature</directory>
  <directory>app-modules/*/Tests/Unit</directory>
</testsuite>

My TestCase.php looks like:

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use LazilyRefreshDatabase;
}

Then I import the test case into Pest.php

No namespaces or class is required.

use Tests\TestCase;

uses(
    TestCase::class,
)->in(__DIR__);

Then in a module I have import uses(Tests\TestCase::class); then write normal pest tests

<?php

use Dcblogdev\Contacts\Models\Contact;

uses(Tests\TestCase::class);

test('can see contacts', function () {

    Contact::factory()->count(10)->create();

    $this->assertDatabaseCount('contacts', 10);

    $this->get('contacts')->assertOk();
});

test('can create a contact', function () {

    $this->post('contacts', [
        'name' => 'test',
    ])->assertRedirect('contacts');

    $this->assertDatabaseCount('contacts', 1);
});
vishytk commented 1 year ago
uses(Tests\TestCase::class);

I have almost the same settings.

The Test file Modules/hms-post/tests/Unit/ExampleTest.php

<?php

use Tests\TestCase;

uses(TestCase::class);

test('that true is true', function () {
    expect(true)->toBeTrue();
});

and when I run the tests, I get following output

   PASS  Modules\hmspost\tests\Unit\ExampleTest
  ✓ that true is true                                                                                       0.05s  

I changed the Example Feature test to following

<?php

namespace Talkad\HmsPost\Tests\Feature;

use Tests\TestCase;

class ExampleTest extends TestCase
{
    /** @test */
    public function the_application_returns_a_successful_response(): void
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

and I get following output

   PASS  Talkad\HmsPost\Tests\Feature\ExampleTest
  ✓ the application returns a successful response                                                           0.07s  
inkomomutane commented 6 months ago

just run this code before on terminal


php artisan modules:sync 
Hennest commented 6 months ago

I think this is a much better solution

// in the root project test/Pest.php

uses(TestCase::class, RefreshDatabase::class)->in('Feature', '../app-modules/*/tests');

instead of including the TestCase::class trait in every test file

mra-dev commented 5 months ago
// in the root project test/Pest.php

uses(TestCase::class, RefreshDatabase::class)->in('Feature', '../app-modules/*/tests');

Confirm, this is working in a fresh Laravel 11 installation. Thanks @Hennest 🙏

bartdenhoed commented 1 month ago

Is anyone else have problems with test outside modules? The tests in the default tests folder of my application root is not loaded when I add the following to the phpunit.xml:

        <testsuite name="Modules">
            <directory suffix="Test.php">./app-modules/*/tests</directory>
        </testsuite>
inmanturbo commented 3 weeks ago

Is anyone else have problems with test outside modules? The tests in the default tests folder of my application root is not loaded when I add the following to the phpunit.xml:

        <testsuite name="Modules">
            <directory suffix="Test.php">./app-modules/*/tests</directory>
        </testsuite>

did you add it manuallly or by running php artisan modules:sync? Can you share the contents of the rest of your phpunit.xml file?

bartdenhoed commented 2 weeks ago

did you add it manuallly or by running php artisan modules:sync? Can you share the contents of the rest of your phpunit.xml file?

I almost dare not say it, but my problem was that the once() (https://pestphp.com/docs/filtering-tests#content-only) method was used on one of the tests... So that was the reason for the strange behavior in my application.