[Bug]: Pest does not find the route
What Happened
Hi, I am using Laravel 11 with Sail for local development and I run the tests through the containers (sail artisan test).
The problem that only the first test that is executed finds the indicated route, even if they are the same.
The second tests returns a 404 error from the ->get().
How to Reproduce
- Install Laravel 11
- Create a route to return something
- Use Pest to test that route two times:
it('test 1', function () {
$this->get('test')->assertStatus(200);
});
it('test 2', function () {
$this->get('test')->assertStatus(200);
});
Result:
sail artisan test
FAIL Tests\Feature\Api\Home\HomeControllerTest
✓ it test 1
⨯ it test 2 <-- Error 404
if I run one by one, it works without problems:
sail artisan test --filter="Test 1"
PASS Tests\Feature\Api\Home\HomeControllerTest
✓ it test 1
sail artisan test --filter="Test 2"
PASS Tests\Feature\Api\Home\HomeControllerTest
✓ it test 2
Also using --parallel we have the same issue.
Sample Repository
No response
Pest Version
3.5.1
PHP Version
8.3.4
Operation System
Linux
Notes
No response
@adrianbeniel did you find a solution? Im facing the same
Thanks for the reply @adrianbeniel ,
This is my api route Route::apiResource('tickets', TicketController::class);
And this is my Feature/TicketTest.php `<?php
test('user can create a ticket', function () { $payload = [ 'title' => fake()->sentence(), 'description' => fake()->paragraph(), 'status' => 'A', ];
$response = $this->actingAsUser()->postJson(route('api.v1.tickets.store'), $payload);
$response->assertStatus(200)
->assertJsonStructure([
'data'
])
->assertJsonFragment([
'status' => 'success',
'title' => $payload['title'],
'description' => $payload['description'],
'status' => $payload['status'],
]);
});
test('user can create a ticket again', function () { $payload = [ 'title' => fake()->sentence(), 'description' => fake()->paragraph(), 'status' => 'A', ];
$response = $this->actingAsUser()->postJson(route('api.v1.tickets.store'), $payload);
$response->assertStatus(200)
->assertJsonStructure([
'data'
])
->assertJsonFragment([
'status' => 'success',
'title' => $payload['title'],
'description' => $payload['description'],
'status' => $payload['status'],
]);
});`
The API endpoint api.v1.tickets.store is visible when i do php artisan route:list
I updated Laravel 11 to Laravel 12. Still the same issue! Also downgraded Pest from 3.7 to 3.0. Still the same issue! Also tried this Pest.php. Still no luck `<?php
use Tests\TestCase;
uses(TestCase::class)->in('Feature', 'Unit');`
I assume that you have a v1.php file and a tickets.php file that you require in the api.php file, right? Be sure to requuire using require instead of require_once.
@alibori Thank you very much. Saved my day :) this solved the issue. @adrianbeniel Its resolved by implementing above changes! Thanks :)
I can confirm that the error can be fixed using require instead of require_once. Thank you!