fix(snapshot): treat empty string as valid snapshot#10188
fix(snapshot): treat empty string as valid snapshot#10188hi-ogawa merged 8 commits intovitest-dev:mainfrom
Conversation
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Note: This PR intentionally doesn't include a browser test for the empty aria snapshot case. The test requires 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. |
hi-ogawa
left a comment
There was a problem hiding this comment.
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
da79634 to
faaf11e
Compare
|
Thanks for the suggestion! Added a test using the KV domain snapshot fixture — |
hi-ogawa
left a comment
There was a problem hiding this comment.
Can you do same for test/snapshots/test/fixtures/domain/basic.test.ts?
|
Done! Added |
hi-ogawa
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
!== undefinedchecks 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). |
Description
toMatchAriaInlineSnapshotcannot match empty aria trees #10158toMatchAriaInlineSnapshot('')treats an empty string as "no snapshot provided"because
''is falsy in JavaScript. This makes it impossible to assert that anelement has an empty aria tree (e.g. all content is
aria-hidden="true").The fix replaces 5 truthiness checks (
if (x),!!x,x ?,x &&) withexplicit
!== undefinedchecks inchai.ts,client.ts, andstate.ts.This aligns the domain snapshot path with the non-domain path, which already
uses
expected !== undefinedatstate.ts:516.Testing
Verified using the KV domain snapshot fixture (non-aria):
parseExpectedempty snapshottest case withtoMatchKvInlineSnapshot(\`)`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
""(empty string) is distinguishedfrom
undefined(no snapshot)toMatchInlineSnapshot,toMatchSnapshot) areunaffected; they already use the correct
!== undefinedpatternPlease don't delete this checklist! Before submitting the PR, please make sure you do the following: