Skip to content

apiSpy Fixture

The apiSpy fixture provides access to captured API requests and responses within your tests.

Usage

import { testWithApiSpy as test, expect } from 'playwright-api-spy';

test('example', async ({ request, apiSpy }) => {
  await request.get('/users');

  console.log(apiSpy.lastRequest);
  console.log(apiSpy.lastResponse);
});

Properties

requests

Array of all captured requests.

const allRequests = apiSpy.requests;
expect(apiSpy.requests).toHaveLength(3);

entries

Array of all captured entries (request + response pairs).

const allEntries = apiSpy.entries;

lastRequest

Most recently captured request.

expect(apiSpy.lastRequest?.method).toBe('POST');
expect(apiSpy.lastRequest?.path).toBe('/users');

lastResponse

Most recently captured response.

expect(apiSpy.lastResponse?.status).toBe(200);
expect(apiSpy.lastResponse?.duration).toBeLessThan(1000);

lastEntry

Most recently captured entry (request + response).

const entry = apiSpy.lastEntry;
console.log(entry?.request, entry?.response);

isPaused

Whether capturing is currently paused.

expect(apiSpy.isPaused).toBe(false);

Methods

addContext(context: string)

Add context annotation to subsequent requests.

apiSpy.addContext('Creating test user');
await request.post('/users', { data: { name: 'Test' } });
apiSpy.clearContext();

clearContext()

Clear the context annotation.

pause()

Pause request capturing.

apiSpy.pause();
await request.get('/noisy-endpoint'); // Not captured
apiSpy.resume();

resume()

Resume request capturing.

clear()

Clear all captured requests.

apiSpy.clear();
expect(apiSpy.requests).toHaveLength(0);

Hooks

onRequest(callback)

Called when a request is made.

apiSpy.onRequest((req) => {
  console.log(`Request: ${req.method} ${req.path}`);
});

onResponse(callback)

Called when a response is received.

apiSpy.onResponse((req, res) => {
  if (res.duration > 1000) {
    console.warn(`Slow: ${req.path} took ${res.duration}ms`);
  }
});

onError(callback)

Called when a request fails.

apiSpy.onError((req, error) => {
  console.error(`Failed: ${req.path} - ${error.message}`);
});