vitest icon indicating copy to clipboard operation
vitest copied to clipboard

`toMatchAriaInlineSnapshot` cannot match empty aria trees

Open mrginglymus opened this issue 7 months ago • 10 comments

Describe the bug

Thanks for adding this into core vitest! In the process of switching from my own implementation I've found that vitest's is unable to assert on an empty aria document:

<button aria-hidden="true">Hidden</button>

as

expect(document.body).toMatchAriaInlineSnapshot('');

is intepreted as being a 'fill this in later' call. I think I also encountered an issue with the non-inline variation where an empty string was not considered a valid yaml document.

Reproduction

https://github.com/vitest-dev/vitest/compare/main...mrginglymus:vitest:empty-aria?expand=1

System Info

System:
    OS: Linux 6.6 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
    CPU: (24) x64 Intel(R) Core(TM) i9-10920X CPU @ 3.50GHz
    Memory: 30.00 GB / 31.24 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 22.15.1 - /home/bill/.nvm/versions/node/v22.15.1/bin/node
    npm: 10.9.2 - /home/bill/.nvm/versions/node/v22.15.1/bin/npm
    pnpm: 10.31.0 - /home/bill/.nvm/versions/node/v22.15.1/bin/pnpm
  npmPackages:
    @vitejs/plugin-basic-ssl: ^2.1.4 => 2.1.4
    @vitest/browser: workspace:* => 4.1.4
    @vitest/browser-playwright: workspace:* => 4.1.4
    @vitest/browser-preview: workspace:* => 4.1.4
    @vitest/browser-webdriverio: workspace:* => 4.1.4
    @vitest/bundled-lib: link:./bundled-lib => undefined
    @vitest/cjs-lib: link:./cjs-lib => undefined
    playwright: catalog: => 1.59.0
    vitest: workspace:* => 4.1.4
    vitest-browser-react: ^2.0.5 => 2.0.5

Used Package Manager

pnpm

Validations

mrginglymus avatar Apr 17 '26 13:04 mrginglymus

I think I understand the intent but this might conflict with the nature of default children partial matching https://vitest.dev/guide/browser/aria-snapshots.html#partial-matching-default and the asserted "root" is always assumed to be the list of something. Not sure yet what should be the expected behavior, but thanks for bringing it up.

hi-ogawa avatar Apr 17 '26 13:04 hi-ogawa

Fair point on partial matching - perhaps just a toHaveEmptyAriaTree(), toHaveNoAccessibleRoles() or something would help fill this gap.

mrginglymus avatar Apr 17 '26 13:04 mrginglymus

Partial behavior can be surprising compared to normal snapshots, so it may be worth calling out more.

Also, the current behavior of empty aria tree is basically broken since no Idempotency for toMatchAriaSnapshot and also unexpected update for toMatchAriaInlineSnapshot, so that's a clear bug at least.

hi-ogawa avatar Apr 20 '26 01:04 hi-ogawa

Hmm, it looks like playwright managed to support this (though technically it also exhibits some behaviors not following partial matching.)

In our case, our snapshot mechanism normally trim and empty string to be considered "no snapshot", so this might be a matter of changing all if (!!snapshot) ... with if (typeof snapshot === 'string') ... like condition.

https://github.com/vitest-dev/vitest/blob/f622716f3f2ae162382a65ddc460e0b0a79d6626/packages/snapshot/src/port/state.ts#L583

hi-ogawa avatar Apr 20 '26 01:04 hi-ogawa

I can create a new issue, but is there any chance of a 'global' deep-equal option? In my case I'm testing very small atoms where I always want complete and exact matches, and having to manually add /children: deep-equal to every snapshot is an unreliable burden. Either as a second argument (.toMatchAriaInlineSnapshot('...', { children: 'deep-equal' })) or alternative assertion (toExactlyMatchAriaInlineSnapshot('...'))?

mrginglymus avatar Apr 21 '26 07:04 mrginglymus

Such mode isn't available, but probably it can be built up as custom snapshot matcher using ivya directly

  • https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers
  • https://github.com/vitest-dev/ivya/blob/master/src/aria/index.ts
    • this is what Vitest uses in https://github.com/vitest-dev/vitest/blob/main/packages/browser/src/client/tester/aria.ts
import { expect, test, Snapshots } from 'vitest'
import { generateAriaTree, renderAriaTree } from "ivya/aria"

expect.extend({
  toExactlyMatchAriaInlineSnapshot(actual: Element, expectedAria?: string) {
    const actualAria = renderAriaTree(generateAriaTree(actual))
    return Snapshots.toMatchInlineSnapshot.call(this, actualAria, expectedAria)
  },
})

test('my aria', () => {
  expect(document.body).toExactlyMatchAriaInlineSnapshot(`...`)
})

If you have request on proper support, please create a separate new feature request.

hi-ogawa avatar Apr 21 '26 07:04 hi-ogawa

Thanks!

mrginglymus avatar Apr 21 '26 07:04 mrginglymus

Or this could be also basically a variant of custom snapshot serializer https://vitest.dev/guide/snapshot.html#custom-serializer

import { generateAriaTree, renderAriaTree } from "ivya/aria"

expect.addSnapshotSerializer({
  test(val) {
    return val && Object.prototype.hasOwnProperty.call(val, '__exact_aria__')
  },
  serialize(val, config, indentation, depth, refs, printer) {
    return renderAriaTree(generateAriaTree(val.__exact_aria__))
  },
})

test('my aria', () => {
  expect({ __exact_aria__: document.body }).toMatchInlineSnapshot(`...`)
})

I haven't tested any of this. Please let me know if any of works 😄

hi-ogawa avatar Apr 21 '26 07:04 hi-ogawa

@hi-ogawa thanks for the hints, gave me some ideas to explore. Ideally I'd like to still use the dynamic matching, just have strict children by default. However, I think there's a bug (or missing feature) upstream, so I've raised https://github.com/vitest-dev/ivya/pull/14 and we can discuss that there.

mrginglymus avatar Apr 21 '26 09:04 mrginglymus

Opened a fix in #10188.

mayrang avatar Apr 24 '26 15:04 mayrang