vincent15000's avatar

Disappointed by Pest

Hello,

It's the first time I write tests with Pest.

And I'm quite disappointed because once in two the tests take 10 seconds or 1 second without any modification.

Who has already had this problem ?

For example this tests take time.

test('superadmin can update one of his users', function () {
    $user1 = createUsers(RoleEnum::SUPERADMIN);
    $user2 = createUsers(RoleEnum::WORKER, $user1->customer);

    $userToUpdate = [
        'name' => 'Test User',
        'email' => '[email protected]',
        'role' => RoleEnum::WORKER->value,
        'customer_id' => $user1->customer_id,
    ];

    actingAs($user1)
        ->put(route('users.update', ['user' => $user2]), $userToUpdate)
        ->assertRedirectToRoute('users.index');

    $this->assertDatabaseHas('users', $userToUpdate);
});
function createUsers(RoleEnum $role = RoleEnum::WORKER, ?Customer $customer = null, int $count = 1): User|Collection
{
    return Event::fakeFor(function () use ($role, $count, $customer) {
        $customer = $customer ?? Customer::factory()->create();

        $factory = User::factory()->for($customer);

        if ($count === 1) {
            $factory = $factory->create([
                'role' => $role,
            ]);
        } else {
            $factory = $factory->count($count)->create([
                'role' => $role,
            ]);
        }

        return $factory;
    });
}

Even with parallel testing it takes much time.

Thanks for your answer.

V

0 likes
2 replies
Tray2's avatar
Tray2
Best Answer
Level 73

I think you went over the ocean to get water on this test of yours.

I've been using Pest for quite some time, and never noticed any slowness other than the one I myself have created.

This is an example from one of my codebases on how I test the update functionality.

it('updates a valid game', function () {
    $validGame = $this->validGame;
    $validGame['title'] = 'Some New Title';
    actingAs($this->user)->put(route('games.update', $this->game), $validGame)
        ->assertRedirect(route('games.index'));
    assertDatabaseHas('games', ['title' => 'Some New Title']);
    assertDatabaseCount('games', 1);
});

The validGame is generated with a beforeEach, I also use FastRefreshDatabase to speed things up.

1 like
vincent15000's avatar

Ok thank you for your response.

I will try to understand what happens with my tests.

I find that Pest is more pratice than PHPUnit, so it would be a shame not persisting to use it.

Please or to participate in this conversation.