Modern Browser Testing and Sharding with Pest 4
Overview of Pest 4 Capabilities
Prerequisites and Environment Setup
To follow this tutorial, you should have a solid grasp of
Key Libraries & Tools
- Pest 4: The core testing framework forPHP.
- Playwright: The high-performance engine driving the browser interactions.
- Laravel: The application framework used for the live examples.
- Inertia.js: Used in the demo to show howPest 4handlesReactandJavaScriptheavy front-ends.
Browser Testing Walkthrough
The syntax for browser testing in get() request used in feature tests, you use visit(). This triggers the
test('user can login', function () {
$user = User::factory()->create();
$this->visit('/')
->click('Login')
->type('email', $user->email)
->type('password', 'password')
->press('Login')
->assertSee('Dashboard');
});
One of the most impressive features is the shared memory state. In older tools, your browser and your test run in separate processes, making it hard to use RefreshDatabase. Notification::fake() and then verify a notification was sent after a browser click—all within the same test block.
Advanced Debugging and Visual Diffs
Debugging browser tests has historically been a "guess and check" process. debug() and tinker() methods to stop this cycle. Placing ->debug() before a failing assertion pauses the test and focuses the browser window so you can see exactly what is wrong. The ->tinker() method is even more powerful; it opens a
Visual Regression Testing
Visual testing is now a first-class citizen. By calling assertScreenshotMatches(),
test('homepage visual regression', function () {
$this->visit('/')
->assertScreenshotMatches();
});
If a single pixel is out of place due to a --diff flag to open a side-by-side slider in the browser, showing exactly what changed in red.
Syntax Notes and Modern Features
assertNoSmoke() method is a powerful shorthand that automatically visits all routes in your application and asserts that there are no console.log statements left behind.
Another syntax feature is the device configuration. You can easily test how your site looks on different viewports by chaining methods like onMobile() or specifying exact hardware like onIphone15(). You can even toggle onDarkMode() to ensure your
Sharding for GitHub Actions
As test suites grow, execution time often becomes a bottleneck. --shard=1/5 flag, you tell
Tips & Gotchas
- Database Isolation: Always use the
RefreshDatabasetrait when testing browser flows that modify data. BecausePest 4shares the connection, your database state stays in sync. - Automatic Waiting: You no longer need to manually code
waitForText().Pest PHPautomatically waits for redirects and element visibility, reducing flakiness. - Parallel Execution: Use the
--parallelflag locally to maximize your CPU cores.Pest PHPandPlaywrightboth support parallel execution, often making browser tests run five times faster than sequentialLaravel Duskruns.
