Skip to main content

API reference

This page documents every public symbol exposed by `@krakentech/blueprint‑playwright.


createPlaywrightHelpers​

Factory that returns test and expect enhanced with custom fixtures.

Import​

import { createPlaywrightHelpers } from '@krakentech/blueprint-playwright';

Usage​

const { test, expect } = createPlaywrightHelpers({
nextServerOptions: { /* PlaywrightNextServerOptions */ },
graphqlEndpoint: process.env.NEXT_PUBLIC_KRAKEN_API_URL,
});

Signature​

function createPlaywrightHelpers(
options: CreatePlaywrightHelpersOptions,
): {
test: typeof playwright.test;
expect: typeof playwright.expect;
}

CreatePlaywrightHelpersOptions​

OptionTypeDefaultDescription
nextServerOptionsPlaywrightNextServerOptions–Parameters to boot a Next.js server per worker.
graphqlEndpointstring–Real GraphQL endpoint to use as fallback when a middleware call is not mocked.
defaultEdgeConfigEdgeConfigItems{}Initial data for the Edge Config store mock.
defaultFlagsEdgeConfigItems{}Initial data for the feature‑flags endpoint mock.
defaultClientHandlersRequestHandler[][]MSW handlers automatically applied to every client request.
defaultServerHandlersRequestHandler[][]MSW handlers automatically applied to every server request.
KeyTypeDescription
dirstringPath to your Next.js root.
devbooleanRun the server in dev mode (next dev) instead of production build. To be used with an env variable like process.env.NODE_ENV === "development".
quietbooleanSilence Next.js logs.
experimentalHttpsServerbooleanEnable the experimental HTTPS server.
confNextConfig(optional) Pass an already‑imported next.config. Needed when used with next.config.ts.

Fixtures​

All fixtures are injected automatically in the Playwright context once you use test from the helpers file; no extra imports required. They are grouped by scope (worker vs test).

Worker‑scope​

NameDescription
_nextServerBoots and stops a Next.js server in the same worker.
_edgeProxyStarts an Express server that exposes a /playwright-edge-server endpoint. It's used by middleware to avoid requests to the real GraphQL endpoint.
_serverWorkersetupServer instance that mocks every Node fetch/XHR.
connectOptionsPatches wsEndpoint so the VSCode Playwright extension can attach even when MSW is intercepting sockets.

All worker fixtures have auto: true, so they start before the first test of the file and stop once the worker ends.

Test‑scope​

NameDescription
_environmentAdds x-playwright-edge-port header so your middleware can reach the local proxy.
serverWorkerSame instance as _serverWorker but reset on every test. Use serverWorker.use() to add handlers.
clientWorkerConverts any MSW handler to page.route. Use clientWorker.use() to mock browser calls.
edgeConfigedgeConfig.use(data) – mocks Edge Config /items and /item/:key.
flagsflags.use(data) – mocks /api/vercel/flags.
waitForGraphQLResponseAwait the next GraphQL response containing a specific operation.
baseURLString with the local Next.js server URL (e.g. http://127.0.0.1:46001).

Example​

test('mock all the things', async ({ page, serverWorker, clientWorker, edgeConfig, flags }) => {
// Server‑side mock
serverWorker.use(
graphql.query('GetSettings', () =>
HttpResponse.json({ data: { settings: { theme: 'dark' } } }),
),
);

// Browser mock
await clientWorker.use(
http.get('/api/notifications', () =>
HttpResponse.json({ data: [] }),
),
);

// Edge Config & feature flags
edgeConfig.use({ featureX: 'A' });
flags.use({ newOnboarding: true });

await page.goto('/');
...
});

Utility helpers​

playwrightMiddleware​

Helper that picks the correct GraphQL endpoint inside Next.js middleware (Edge Runtime).

Import​

import { playwrightMiddleware } from '@krakentech/blueprint-playwright/middleware';

Usage​

export function middleware(req: NextRequest) {
const { graphqlEndpoint } = playwrightMiddleware({
req,
enabled: !!process.env.NEXT_PUBLIC_PLAYWRIGHT_MODE, // If you plan to use this variable client side, remember to add the NEXT_PUBLIC_ prefix
defaultGraphqlEndpoint: process.env.NEXT_PUBLIC_KRAKEN_API_URL,
});

// your auth / proxy logic here …
}

During tests the helper detects the x-playwright-edge-port header set by _environment and rewrites the endpoint to the local proxy. In production it simply returns the defaultGraphqlEndpoint.

Gotchas & Tips​

Multiple Set‑Cookie headers​

When mocking a response that sets multiple cookies, use the Headers constructor:

return HttpResponse.json(
{ data: 'ok' },
{
headers: new Headers([
['Set-Cookie', 'access=token; Path=/; HttpOnly'],
['Set-Cookie', 'refresh=token; Path=/; HttpOnly'],
]),
},
);

clientWorker will parse and add all cookies to the Playwright browser context.

Debugging with VSCode​

If you use the VSCode Playwright extension, keep connect mode enabled – the connectOptions fixture rewrites the wsEndpoint from localhost to 127.0.0.1 so MSW can forward the connection correctly.