Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Has anyone tried using Pest with this package ? #52

Open
vishytk opened this issue Jul 31, 2023 · 9 comments
Open

Has anyone tried using Pest with this package ? #52

vishytk opened this issue Jul 31, 2023 · 9 comments

Comments

@vishytk
Copy link

vishytk commented Jul 31, 2023

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
Copy link
Contributor

inxilpro commented Aug 9, 2023

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
Copy link

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
Copy link
Author

vishytk commented Aug 15, 2023

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
Copy link

just run this code before on terminal

php artisan modules:sync 

@Hennest
Copy link

Hennest commented Feb 29, 2024

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
Copy link

mra-dev commented Mar 28, 2024

// 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
Copy link

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
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants