Getting started
Set up Playwright mocks
Mock a server-side request and check the page it renders. You’ll use Mock Service Worker (MSW) handlers through @octopus-energy/playwright, without changing your application’s data-fetching code for tests.
Playwright’s browser interception doesn’t intercept requests made by your Next.js server. These helpers use Next.js’s experimental test proxy to apply your mocks to server requests as well.
Choose your MSW version
Use the entry point that matches the MSW major installed in your application:
| MSW version | Entry point | Additional dependency |
|---|---|---|
| MSW 2 | @octopus-energy/playwright/msw/v2 | None |
| MSW 3 | @octopus-energy/playwright/msw/v3 | graphql |
Both entry points export createPlaywrightHelpers and MSW, GraphQL, and
factory-specific types only. Shared Global Config and Flags helpers compose
with either factory, with unchanged signatures and behavior. Import them from
their domain entry points, not /msw/v2 or /msw/v3:
| Entry point | Exports |
|---|---|
@octopus-energy/playwright/setup | composeGlobalSetup, Cleanup, Setup |
@octopus-energy/playwright/blueprint-auth | withBlueprintAuth, createSigningKeysSetup, authentication types |
@octopus-energy/playwright/global-config | withGlobalConfig |
@octopus-energy/playwright/flags/global-config | withGlobalConfigFlags, WithGlobalConfigFlagsOptions |
@octopus-energy/playwright/flags/vercel | withVercelFlags |
@octopus-energy/playwright/edge-config | withEdgeConfig (deprecated) |
@octopus-energy/playwright/flags/blueprint | withBlueprintFlags, WithBlueprintFlagsOptions (deprecated) |
The MSW version tabs below share your selection across the guides and API reference.
Migrating existing MSW 2 tests
The root @octopus-energy/playwright API entry point has been removed. Move
createPlaywrightHelpers and its version-specific type imports to
@octopus-energy/playwright/msw/v2, and shared helper and options type imports to the domain
entry points above. Keep msw@2 installed. Migrating these imports does not require upgrading to
MSW 3.
Quick start
Install dependencies
Add these testing packages to your Next.js application. Keep next in its runtime dependencies:
npm i -D @octopus-energy/playwright @playwright/test msw@2| Requirement | Version |
|---|---|
| Node.js | ≥24.0.0 |
| Peer dependency | Version | When |
|---|---|---|
@playwright/test | ^1.51.1 | Required |
msw | ^2.13.5 || ^3.0.0 | Required |
next | >=14.2.0 <17.0.0 | Required |
@krakentech/blueprint-auth | ^45.1.0 | Using @octopus-energy/playwright/blueprint-auth |
@octopus-energy/flags | >=0.1.0 | Using withGlobalConfigFlags |
@vercel/edge-config | ^1.1.0 | Using the deprecated withEdgeConfig or withBlueprintFlags fixtures |
@vercel/global-config | ^1.5.1 | Using withGlobalConfig or withGlobalConfigFlags |
flags | ^4.1.0 | Using withGlobalConfigFlags or withVercelFlags |
graphql | >=16 | Using @octopus-energy/playwright/msw/v3 |
This package uses ECMAScript modules (ESM). Set the module type in package.json:
{
"type": "module"
}For browser installation and Playwright setup, follow Playwright’s getting started guide.
Create a page that fetches a name
Fetch a name from a test-only application programming interface (API) endpoint when rendering the page. The test replaces that request, so the endpoint doesn’t need a running service.
Fetch the name in a server component. Use cache: "no-store" to avoid fetching from the test-only endpoint during next build:
export default async function MockUserPage() {
const response = await fetch("https://api.example.test/user", {
cache: "no-store",
});
const user: { name: string } = await response.json();
return <h1>Hello, {user.name}</h1>;
}Enable the Next.js test proxy
Merge this setting into your Next.js configuration. Enable the proxy only for test runs, never deployed traffic:
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
experimental: {
testProxy: process.env.PLAYWRIGHT_TEST === "1" && process.env.VERCEL !== "1",
},
};
export default nextConfig;Without the proxy, server-side requests bypass your MSW handlers. On Vercel, enable system environment variables so the deployment guard can detect VERCEL.
Create shared test helpers
Export the package’s test and Playwright’s expect from one module. Import these helpers in every test that needs request mocks:
import { createPlaywrightHelpers } from "@octopus-energy/playwright/msw/v2";
export const { test, expect } = createPlaywrightHelpers();Configure a dedicated test server
Create this configuration, or merge its settings into your existing Playwright configuration. The explicit opt-in enables the proxy when Playwright starts Next.js:
import { defineConfig } from "@playwright/test";
const baseURL = "http://localhost:3000";
export default defineConfig({
testDir: "./tests/playwright",
use: { baseURL },
webServer: {
command: "pnpm dev",
env: { PLAYWRIGHT_TEST: "1" },
url: baseURL,
reuseExistingServer: false,
},
});Keep reuseExistingServer: false. An existing development server may lack the proxy or retain cached data from another test run.
To test a local production build, use pnpm run build && pnpm run start as webServer.command. The same environment applies to both commands. NODE_ENV alone cannot distinguish a test run from a live deployment.
Register the mock and assert the page
Create the handler before navigating. Its URL matches the server component’s fetch exactly:
import { HttpResponse } from "msw";
import { expect, test } from "../../lib/playwright/test";
test("renders a mocked name", async ({ page, msw }) => {
msw.use(
msw.http.get("https://api.example.test/user", () => {
return HttpResponse.json({ name: "Alex" });
}),
);
await page.goto("/mock-user");
const heading = page.getByRole("heading", { name: "Hello, Alex" });
await expect(heading).toBeVisible();
});The msw.http and msw.graphql factories match the MSW version selected in your shared helper. You can also import factories directly from that version of MSW. Pass defaultHandlers to createPlaywrightHelpers for handlers that apply to every test. See the MSW guide for GraphQL handlers.
Run the test
Run the new test from your application root:
npm exec -- playwright test tests/playwright/mock-user.spec.tsThe test passes when the page displays Hello, Alex. Next.js fetched the name on the server, but your test supplied the response.
Next steps
Choose a guide for the behavior you need to test: