pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: Pest does not find the route

Open adrianbeniel opened this issue 1 year ago • 7 comments

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 avatar Nov 23 '24 10:11 adrianbeniel

@adrianbeniel did you find a solution? Im facing the same

lalithhakari avatar Apr 07 '25 10:04 lalithhakari

@adrianbeniel did you find a solution? Im facing the same Hi!

How are you defining the routes?

alibori avatar Apr 07 '25 12:04 alibori

Thanks for the reply @adrianbeniel ,

Image

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

lalithhakari avatar Apr 07 '25 12:04 lalithhakari

Image

lalithhakari avatar Apr 07 '25 12:04 lalithhakari

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');`

lalithhakari avatar Apr 07 '25 12:04 lalithhakari

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 avatar Apr 07 '25 13:04 alibori

@alibori Thank you very much. Saved my day :) this solved the issue. @adrianbeniel Its resolved by implementing above changes! Thanks :)

lalithhakari avatar Apr 07 '25 13:04 lalithhakari

I can confirm that the error can be fixed using require instead of require_once. Thank you!

adrianbeniel avatar Jul 21 '25 21:07 adrianbeniel