Skip to content

Text snapshots ​

A text snapshot is a YAML rendering of the screen or one element: test IDs, text and state, with the focused element marked. It is deterministic and comparable across platforms, so it serves three purposes:

  • Failure context. Every failure dump includes one; an agent reads it instead of a screenshot.
  • Exploration. app.snapshot() (and the MCP server's snapshot tool) shows what is on screen and what is focusable.
  • Assertions. Compare a region against a stored snapshot to lock down structure.
yaml
- rail-0:
    - rail-0-card-0 "Big Buck Bunny"
    - rail-0-card-1 "Sintel" [focused]
- rail-1:
    - rail-1-card-0 "Tears of Steel"

One line per visible element: its test ID (else its text), its text in quotes and its state in brackets. Exact rules: text snapshots reference.

ts
import { test, expect } from '@toets/test';

test('home rail lists the mocked titles', async ({ app }) => {
  await app.launch();
  await expect(app.getByTestId('rail-0')).toMatchTextSnapshot(`
    - rail-0:
      - rail-0-card-0 "Big Buck Bunny" [focused]
      - rail-0-card-1 "Sintel"
      - rail-0-card-2 "Tears of Steel"
      - rail-0-card-3 "Elephants Dream"
  `);
});

With a string, the snapshot is compared inline (indentation ignored). Without one, it is compared to a baseline file per project, written (and failed) on the first run like any Playwright snapshot and updated with --update-snapshots.

Traps ​

  • Snapshot a region, not the screen. Whole-screen snapshots break on unrelated changes. Pick the element whose structure the test is about.
  • Nesting follows the tree. Black-box native trees can flatten containers (a React Native View lists its children as its siblings), so a snapshot spanning them only has the same shape where the tree nests: declare requires('tree.hierarchy') (the DOM and the SDK tree), or snapshot an element that keeps its children everywhere.
  • Mock the data first. A snapshot of live content changes with the content. Use network mocking.
  • Inline snapshots serve all platforms when the app uses the same test IDs everywhere; where platforms legitimately differ, the diff shows it. Baseline files are per project and never carry the host OS (toets drops Playwright's -darwin / -linux suffix), so baselines written on a laptop match in CI.