Pest's browser testing plugin now supports subdomain testing in Laravel applications through a new withHost() method, addressing a longstanding limitation for developers building multi-tenant and workspace applications.
The Problem
Previously, the test server was coded to 127.0.0.1, making it impossible to test subdomain-based routing patterns. This was particularly challenging for applications using Laravel's domain routing:
Route::domain('{tenant}.localhost')->group(function () { Route::get('/dashboard', function (string $tenant) { return view('dashboard', ['tenant' => $tenant]); });});
Without the ability to specify a host, browser tests couldn't properly match these routes.
The Solution
The new withHost() method allows you to specify the hostname for your tests in two ways.
Configure it per test:
it('shows the tenant dashboard', function () { $page = visit('/dashboard')->withHost('acme.localhost'); $page->assertSee('Welcome to Acme');});
Or set it globally in your Pest.php file:
pest()->browser()->withHost('acme.localhost');
Practical Use Cases
This feature is particularly useful for:
- Laravel Sail environments: Point tests to your Sail hostname like
laravel.testor subdomain variants - Multi-tenant isolation testing: Verify that each tenant only sees their own data by visiting different subdomains in sequence
- Workspace-based applications: Test subdomain routing logic for team or organization-based apps
Getting Started
Update the Pest browser plugin to get access to the new feature:
composer update pestphp/pest-plugin-browser
Read more about this feature in Nuno Maduro's full article.