Saturday, November 21, 2026

Playwright expect(body).toMatchSnapshot() Snapshot Testing

 In Playwright, expect(body).toMatchSnapshot('config-response.json') is used to compare an API response body against a previously saved snapshot file.

It is useful for API response validation, regression testing, and detecting unexpected changes in response data.

Playwright expect(body).toMatchSnapshot() Snapshot Testing


1. Basic example

import { test, expect } from '@playwright/test';

test('Validate API response using snapshot', async ({ request }) => {

    const response = await request.get('/api/config');

    expect(response.status()).toBe(200);

    const body = await response.json();

    await expect(body).toMatchSnapshot('config-response.json');

});

2. How does it work?

First execution:

If the snapshot does not exist, Playwright creates a snapshot file containing the actual API response.

Example snapshot:

config-response.json

{
  "id": 101,
  "name": "Sumit",
  "department": "QA",
  "active": true
}

Second execution:

Playwright compares the actual API response with the saved snapshot.

  • If both match, the test passes.

  • If the response changes, the test fails.

  • If a property is added, removed, or modified, Playwright identifies the difference.

3. Update snapshot

If the API response changes intentionally, update the snapshot using:

npx playwright test --update-snapshots

You can also update only changed snapshots:

npx playwright test --update-snapshots=changed

4. Snapshot with dynamic values

Sometimes API responses contain dynamic values such as timestamps, IDs, or tokens.

You can normalize those values before comparing snapshots.

test('API snapshot with dynamic values', async ({ request }) => {

    const response = await request.get('/api/config');

    const body = await response.json();

    // Normalize dynamic values
    body.id = 0;
    body.createdAt = '<TIMESTAMP>';
    body.token = '<TOKEN>';

    await expect(body).toMatchSnapshot('config-response.json');

});

5. Snapshot with selected response properties

You don't always need to compare the entire API response.

test('Validate selected API response properties', async ({ request }) => {

    const response = await request.get('/api/config');

    const body = await response.json();

    const config = {
        name: body.name,
        department: body.department,
        active: body.active
    };

    await expect(config).toMatchSnapshot('config-response.json');

});

6. Difference between toMatchSnapshot() and toHaveScreenshot()

Method

Purpose

toMatchSnapshot()

Compares data, strings, buffers, and other snapshot-supported values

toHaveScreenshot()

Compares screenshots of web pages or elements

toMatchAriaSnapshot()

Compares accessibility tree snapshots

7. Snapshot file location

By default, Playwright stores snapshots in a directory associated with the test file.

Example:

tests/
  api/
    config.spec.ts
    config.spec.ts-snapshots/
      config-response.json

Important: toMatchSnapshot() is an asynchronous assertion, so use await.

Recommendation: For API regression testing, use toMatchSnapshot() when you want to validate the complete response structure. For individual fields, use regular assertions such as toBe(), toEqual(), and toContain().

No comments:

Post a Comment