Does Fastify Work With Playwright?
Fastify and Playwright work together seamlessly—use Fastify to build your application and Playwright to test it end-to-end.
Quick Facts
How Fastify Works With Playwright
Fastify and Playwright are complementary tools that don't depend on each other. Fastify serves as your application backend, while Playwright runs as a separate process to automate browser testing against that running server. The typical workflow involves starting a Fastify server (often on localhost), then using Playwright to spawn browsers and interact with your application through its HTTP endpoints.
Developers appreciate this combination because Fastify's startup speed and low overhead make test suites faster—the server boots quickly for each test run. Playwright handles all the browser automation, so you get true end-to-end testing with real browser rendering, JavaScript execution, and network conditions. You'll manage the server lifecycle in your test setup/teardown hooks, typically using beforeAll/afterAll in your test framework.
The architecture is straightforward: Fastify runs on one port, Playwright connects to it via localhost URLs. For CI/CD pipelines, this is especially clean since both tools are Node.js-based and don't require external services. No special plugins or middleware are needed on the Fastify side—just expose your application normally.
Best Use Cases
Quick Setup
npm install fastify playwright @playwright/test// server.js
const fastify = require('fastify')();
fastify.get('/', async () => ({ message: 'Hello' }));
fastify.listen({ port: 3000 });
// test.spec.js
const { test, expect } = require('@playwright/test');
test('homepage loads', async ({ page }) => {
await page.goto('http://localhost:3000');
const heading = await page.locator('text=Hello').isVisible();
expect(heading).toBeTruthy();
});
// playwright.config.js
module.exports = {
use: { baseURL: 'http://localhost:3000' },
webServer: {
command: 'node server.js',
url: 'http://localhost:3000',
reuseExistingServer: false,
},
};Known Issues & Gotchas
Server not ready when tests start
Fix: Use Fastify's .ready() promise or add a health-check endpoint that Playwright polls before running tests
Port conflicts in parallel test execution
Fix: Use dynamic port assignment (port 0) or a port manager library, and pass the actual port to Playwright programmatically
Stale server state between tests
Fix: Reset database state and clear caches between test runs using beforeEach hooks or consider spinning up fresh server instances per test
Playwright timeouts on slow Fastify routes
Fix: Configure Playwright timeouts appropriately and monitor Fastify performance—use Fastify's built-in logging to identify slow handlers
Alternatives
- •Express.js with Cypress—similar setup but Cypress is browser-specific and more opinionated
- •Next.js with Playwright—full-stack framework with built-in server; good for SSR applications
- •Koa.js with Selenium—lighter framework option, though Selenium is less modern than Playwright
Resources
Related Compatibility Guides
Explore more compatibility guides