Skip to content

Lesson 3 of 9 · 19m

Pest Without Losing Rigor

What Pest changes ergonomically, what it does not change architecturally, and how to use datasets, expectations, and plugins without writing fuzzy tests.

Pest is a wrapper around PHPUnit, not an alternative testing philosophy. It removes boilerplate and makes small examples pleasant to read. It does not make loose assertions precise, it does not isolate your database for you, and it absolutely does not mean architecture stopped mattering.

The syntax is the small win

it('applies no discount below the threshold', function () {
    expect(Discount::forSubtotal(999))->toBe(0);
});

it('rejects negative quantities', function () {
    new OrderLine(quantity: -1, unitPrice: Money::usd(500));
})->throws(InvalidQuantity::class, 'quantity must be positive');

That is PHPUnit underneath. The generated test cases still run through vendor/bin/pest, and PHPUnit attributes, extensions, and most configuration concepts still apply.

Datasets mirror PHPUnit data providers

it('calculates discounts', function (int $subtotal, int $expected) {
    expect(Discount::forSubtotal($subtotal))->toBe($expected);
})->with([
    'no discount below threshold' => [999, 0],
    'ten percent wholesale' => [10000, 1000],
]);

The same discipline applies: if every dataset row calls the same code with different numbers, a dataset is perfect. If every row needs different mocks and assertions, the rows are different tests pretending to be one test.

Expectations still need precision

Pest makes it easy to write fluent chains:

expect($user)
    ->toBeInstanceOf(User::class)
    ->and($user->name())->toBe('Ada')
    ->and($user->isActive())->toBeTrue();

Do not drift into toEqual because it is easier. toBe is strict identity for scalars and objects. toEqual is comparative. toMatchArray is useful for API payloads, but it deliberately ignores array shape outside the keys you specify. That is a choice, not a default.

Keep tests boring when behavior gets complicated

Pest's beforeEach, higher-order tests, and architecture testing plugin are useful, but cleverness compounds. A closure that builds the entire universe in beforeEach makes every failure harder to inspect.

beforeEach(function () {
    $this->clock = new FixedClock(new DateTimeImmutable('2026-01-15T12:00:00Z'));
});

Shared setup should create shared context, not hide the scenario. Arrange the important inputs inside the test body where the reader can see them.

Field notes

  • Run vendor/bin/pest --parallel only after the serial suite is deterministic; parallel execution exposes shared-state bugs.
  • Pest architecture tests can enforce namespace and dependency rules, but they are architecture checks, not substitutes for behavior tests.
  • Snapshot expectations are dangerous for large HTML output. They capture noise and train reviewers to click accept.
  • If the team knows PHPUnit deeply, adopt Pest incrementally. A mixed suite is worse than either style alone.

Was this lesson helpful?

Sign in to rate