Open vishytk opened 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…
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);
});
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
just run this code before on terminal
php artisan modules:sync
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
// 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 🙏
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>
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 thephpunit.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?
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.
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 ofapp-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 ofTalkad\HmsPost\Tests\Unit\ExampleTest
The ExampleTest (modules/hms-post/tests/Unit/ExampleTest.php)
The Pest configuration
tests/Pest.php under the main Laravel tree
Need help on this.