Skip to content

fix(snapshot): treat empty string as valid snapshot#10188

Merged
hi-ogawa merged 8 commits intovitest-dev:mainfrom
mayrang:fix/empty-aria-inline-snapshot
Apr 27, 2026
Merged

fix(snapshot): treat empty string as valid snapshot#10188
hi-ogawa merged 8 commits intovitest-dev:mainfrom
mayrang:fix/empty-aria-inline-snapshot

Conversation

@mayrang
Copy link
Copy Markdown
Contributor

@mayrang mayrang commented Apr 24, 2026

Description

toMatchAriaInlineSnapshot('') treats an empty string as "no snapshot provided"
because '' is falsy in JavaScript. This makes it impossible to assert that an
element has an empty aria tree (e.g. all content is aria-hidden="true").

The fix replaces 5 truthiness checks (if (x), !!x, x ?, x &&) with
explicit !== undefined checks in chai.ts, client.ts, and state.ts.
This aligns the domain snapshot path with the non-domain path, which already
uses expected !== undefined at state.ts:516.

Testing

Verified using the KV domain snapshot fixture (non-aria):

  • Added empty string handling to KV adapter parseExpected
  • Added empty snapshot test case with toMatchKvInlineSnapshot(\`)`
  • Updated integration test assertions to include the new case
  • All 24 snapshot test files / 45 tests pass

A browser test for the empty aria tree case will be added as a follow-up once
ivya supports empty templates (vitest-dev/ivya#17).

Impact

  • No breaking changes — only changes how "" (empty string) is distinguished
    from undefined (no snapshot)
  • Non-domain snapshots (toMatchInlineSnapshot, toMatchSnapshot) are
    unaffected; they already use the correct !== undefined pattern
  • Existing aria snapshot tests remain unchanged and pass as before

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.
  • Read the Contributing Guidelines.
  • PR title follows conventional commits format.

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 24, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 79bf076
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/69ef1faa815ae80008f999c9
😎 Deploy Preview https://deploy-preview-10188--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.

@mayrang
Copy link
Copy Markdown
Contributor Author

mayrang commented Apr 24, 2026

Note: This PR intentionally doesn't include a browser test for the empty aria snapshot case. The test requires parseAriaTemplate('') to return an empty fragment instead of throwing, which is currently an ivya-side limitation.

I've submitted a fix for that at vitest-dev/ivya#17. Once it lands and the dependency is bumped, I'll follow up with a PR adding the test coverage.

Copy link
Copy Markdown
Collaborator

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

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

We have test fixture for non-aria domain snapshot, so we should be able to test the change without waiting for vitest-dev/ivya#17.

Add test coverage using the KV domain snapshot fixture to verify that
empty string '' is treated as a valid snapshot, not as "no snapshot".

- Add empty string handling to KV adapter parseExpected
- Add 'empty snapshot' test case to domain-inline fixture
- Update integration test assertions for the new case
- Update domain-error snapshot line numbers
@mayrang mayrang force-pushed the fix/empty-aria-inline-snapshot branch from da79634 to faaf11e Compare April 25, 2026 12:12
@mayrang
Copy link
Copy Markdown
Contributor Author

mayrang commented Apr 25, 2026

Thanks for the suggestion! Added a test using the KV domain snapshot fixture — toMatchKvInlineSnapshot(\`)with an empty object{}. I also added empty string handling to the KV adapter's parseExpectedto return{}` for empty input.

Copy link
Copy Markdown
Collaborator

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

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

Can you do same for test/snapshots/test/fixtures/domain/basic.test.ts?

@mayrang
Copy link
Copy Markdown
Contributor Author

mayrang commented Apr 25, 2026

Done! Added test('empty snapshot', () => { expect({}).toMatchKvSnapshot() }) to the file-based fixture as well.

@hi-ogawa hi-ogawa self-assigned this Apr 26, 2026
@hi-ogawa hi-ogawa changed the title fix(snapshot): treat empty string as valid domain snapshot fix(snapshot): treat empty string as valid snapshot Apr 27, 2026
hi-ogawa
hi-ogawa previously approved these changes Apr 27, 2026
Copy link
Copy Markdown
Collaborator

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

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

Thanks. I updated kv adapter to actually test empty string, which then revealed pollMatchDomain case wasn't fixed fully, then fixed that. I also added test case for non-domain snapshot and confirmed that's also fixed.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes domain inline snapshot handling so an empty string ("") is treated as a provided snapshot (not “no snapshot provided”), which unblocks assertions like toMatchAriaInlineSnapshot('') for empty aria trees and aligns domain snapshot behavior with existing non-domain snapshot logic.

Changes:

  • Replaced truthiness checks with explicit !== undefined checks in the domain snapshot flow (chai integration, snapshot client, snapshot state) so "" is considered a valid snapshot.
  • Added/updated snapshot fixtures and integration tests to cover empty-string snapshots for domain snapshots and custom serializer output.
  • Updated expected snapshot-file outputs and error snapshots to include the new “empty snapshot” cases.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/snapshots/test/fixtures/domain/basic.ts KV domain adapter updated to render/parse empty snapshots.
test/snapshots/test/fixtures/domain/basic.test.ts Adds an “empty snapshot” domain fixture test.
test/snapshots/test/fixtures/domain-inline/basic.test.ts Adds an inline empty snapshot fixture test.
test/snapshots/test/fixtures/domain-poll/basic.test.ts Adds a polled empty snapshot fixture test.
test/snapshots/test/fixtures/domain-poll-inline/basic.test.ts Adds a polled inline empty snapshot fixture test.
test/snapshots/test/fixtures/custom-serializers-empty/vitest.config.ts New fixture config for empty serializer output coverage.
test/snapshots/test/fixtures/custom-serializers-empty/basic.test.ts New fixture validating empty/whitespace serializer outputs with file/inline snapshots.
test/snapshots/test/fixtures/custom-serializers-empty/snapshots/basic.test.ts.snap New fixture snapshot file covering empty/whitespace file snapshots.
test/snapshots/test/domain.test.ts Updates integration expectations to include the new empty snapshot case.
test/snapshots/test/domain-inline.test.ts Updates integration expectations to include inline empty snapshot usage.
test/snapshots/test/domain-poll.test.ts Updates poll integration expectations to include empty snapshot results.
test/snapshots/test/domain-poll-inline.test.ts Updates poll+inline integration expectations to include empty snapshot usage.
test/snapshots/test/custom-serializers.test.ts Adds integration test coverage for empty serializer output behavior.
packages/vitest/src/integrations/snapshot/chai.ts Treats inlineSnapshot === "" as provided (strip indentation when not undefined).
packages/snapshot/src/client.ts Uses expectedSnapshot.data !== undefined and allows stableResult.rendered === "".
packages/snapshot/src/port/state.ts Marks hasSnapshot true when snapshot data is an empty string (not undefined).

Comment thread test/snapshots/test/fixtures/domain/basic.ts
@hi-ogawa hi-ogawa merged commit e145d57 into vitest-dev:main Apr 27, 2026
16 checks passed
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.

3 participants