Skip to content

feat(experimental): support aria snapshot#9668

Open
hi-ogawa wants to merge 244 commits intovitest-dev:mainfrom
hi-ogawa:feat-aria-snapshot
Open

feat(experimental): support aria snapshot#9668
hi-ogawa wants to merge 244 commits intovitest-dev:mainfrom
hi-ogawa:feat-aria-snapshot

Conversation

@hi-ogawa
Copy link
Copy Markdown
Collaborator

@hi-ogawa hi-ogawa commented Feb 15, 2026

Description

Adds toMatchAriaSnapshot() and toMatchAriaInlineSnapshot() matchers for asserting DOM accessibility trees, inspired by Playwright's aria snapshot feature. Core logic of aria tree generation, parsing, matching are entirely implemented on ivya vitest-dev/ivya#8 as Aria related runtime utility lives there.

Under the hood, this PR also introduces domain snapshot API in Vitest core to allow extending comparison mechanism beyond exact string equality. The simple example is added as a test case and also documented to illustrate the idea. However, I'd treat this API being "experimental" since I haven't too deeply thought through the API shape beyond what I minimally needed for implementing aria snapshot.

API

// file
expect(document.querySelector('nav')).toMatchAriaSnapshot()

// inline
expect(document.body).toMatchAriaInlineSnapshot(`
  - paragraph: Original
  - button "1234": Pattern
`)

// with expect.element
await expect.element(page.getByRole('navigation')).toMatchAriaInlineSnapshot(`
  - button: Save
  - button: Cancel
`)

// with expect.poll (technically works but not documented specifically)
await expect.poll(() => document.body).toMatchAriaInlineSnapshot(`
  - heading "Dashboard" [level=1]
`)

Example

Given HTML like

<p>Original</p>
<button aria-label="1234">Pattern</button>
<p>Extra</p>

Initially generated snapshot looks like:

- paragraph: Original
- button "1234": Pattern
- paragraph: Extra

Snapshot can be manually edited to include regex pattern or remove some part and snapshot assertion continues to pass:

- paragraph: Original
- button /\d+/: Pattern

Now when actual HTML changes to:

<p>Changes</p>        👈 (before: "Original", after: "Changed")
<button aria-label="1234">Pattern</button>
<p>Extra</p>

Snapshot would now fail, but the error diff normalizes partially matching part:

- - paragraph: Original
+ - paragraph: Changed
  - button /\d+/: Pattern

When forcing the snapshot update via --update, newly generated snapshot would reflect the same error diff, which means manually edited part is preserved:

- paragraph: Changed
- button /\d+/: Pattern

TODO

Questions

  • Should poll snapshot with --update wait for stable snapshot?
    • TODO: new heuristics and new option? employ similar strategy as toMatchScreenshot?
      Yes, it should. We can simplify pool more and wait until stable on "new" (initial snaphsot) and "all" (--update) cases
  • How to pull in yaml? (fixed by feat: add minimal YAML parser for aria snapshot templates ivya#13)
    • Aria snapshot requires yaml parser and it's now bundled through @vitest/browser/dist/expect-element.js. The package yaml is huge around 100kb. This caused expect-element.js to go from 23kb to 140kb. Should we try code split and lazily load on runtime?
    • domain snapshot itself is synchronous. lazy load requires making toMatchAriaSnapshot specifically asynchronous, which is technically possible. Or we can make explicit opt-in/out flag to lazy load early during runtime.
    • Or we (AI) can hand-roll simplified yaml parser (note that yaml serializer is already hand-rolled partly by playwright).
      feat: add minimal YAML parser for aria snapshot templates ivya#13

Follow-up considered


TODO

  • design and poc
    • file snapshot
    • inline snapshot
  • aria snapshot adapter
  • support poll + snapshot via domain model
    • file snapshot
    • inline snapshot
    • expect.element + aria snapshot becomes a special case
      • works except (current) webkit by the same reason as defineHelper. should be fixed on next playwright update, which has better async stack.
  • wire up feat: add aria snapshot utilities ivya#8
  • review ai slop
    • aria implementation
    • tests
    • domain snapshot match
    • poll snapshot
    • reduce bloat API
    • credit playwright
  • migrate internal to use snaphsot domain (later)
  • docs
  • PR summary

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@netlify
Copy link
Copy Markdown

netlify bot commented Feb 15, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit f4736ac
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/69d5d5d00d69f70008db9dae
😎 Deploy Preview https://deploy-preview-9668--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@hi-ogawa
Copy link
Copy Markdown
Collaborator Author

hi-ogawa commented Apr 3, 2026

I finished the follow up of providing domain snapshot API via custom matcher. Internally aria snapshot also goes through the same path. #10041 I like this shape better so I will merge this to here after #10042 merged to main.

EDIT: two are merged via hi-ogawa#5

hi-ogawa and others added 9 commits April 7, 2026 08:50
* refactor: use custom matcher for domain snapshot

* test: test toMatchKvSnapshot and toMatchKvInlineSnapshot

* test: update exports

* refactor: aria snapshot via expect.extend

* chore: comment what to do

* feat: support `expect.extend(..., { __vitest_poll_takeover__: true })`

* test: update

* refactor: remove unused

* test: update exports

* refactor: remove more

* test: update

* chore: remove types

* docs: remove addSnapshotDomain

* chore: clean snapshot matcher transition leftovers

Co-authored-by: Codex <noreply@openai.com>

* chore: unused

* docs: update custom snapshot matcher guidance

Co-authored-by: Codex <noreply@openai.com>

* docs: re-order

* docs: jsdoc

* refactor: use __vitest_poll_takeover__ per matcher function

* refactor: revert expect.extend options

* chore: comment

* chore: comment

---------

Co-authored-by: Codex <noreply@openai.com>
@hi-ogawa
Copy link
Copy Markdown
Collaborator Author

hi-ogawa commented Apr 7, 2026

@sheremet-va This is ready yet again.

Copy link
Copy Markdown
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I've seen a multi-line ARIA snapshot - how are they supported?

Otherwise looks good 👍


- **Type:** `() => void`

Captures the accessibility tree of a DOM element and compares it against a stored snapshot. Inspired by [Playwright's ARIA snapshots](https://playwright.dev/docs/aria-snapshots).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inspired or based on?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified the paragraph here and removed the mention.

On first run, Vitest generates a snapshot file entry:

```yaml
- form "Log In":
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it create a separate file or does it have exports[] file? I thought it just follow regular snapshots, but this example makes me doubt it

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Updated with the explicit file with __snapshots__/basic.test.ts.snap.

}
}

// TODO: should `all` mode ignore parse error?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not certain. Silently hiding parse error isn't good either and users can fix or purge the broken snapshot manually and that decision affects how the snapshot get updated since aria snapshot adjusts based on the template side.

I'll leave this as is and see what will happen. I've updated the comment to mention the decision.

@hi-ogawa
Copy link
Copy Markdown
Collaborator Author

hi-ogawa commented Apr 8, 2026

I don't think I've seen a multi-line ARIA snapshot - how are they supported?

Do you mean multiline text like paragraph? ARIA snapshot normalize white space and new lines, so the generated text is always a single line. For example,

<p>
....anything...
...<br />
...
</p>

should always end up

- paragraph: ...anything but normalized...

@hi-ogawa hi-ogawa requested a review from sheremet-va April 8, 2026 04:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose updateSnapshot via SnapshotState [Brower Mode] Support aria snapshot matcher

4 participants