From 8800601d2e319ee7cd35e408a960f78bd24cb340 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 16 Jun 2024 15:20:51 +0900 Subject: [PATCH] fix(snapshot): fix `toMatchFileSnapshot` with empty file (#5894) --- packages/snapshot/src/client.ts | 2 +- test/core/test/fixtures/snapshot-empty.txt | 0 test/core/test/snapshot-file.test.ts | 4 ++++ 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 test/core/test/fixtures/snapshot-empty.txt diff --git a/packages/snapshot/src/client.ts b/packages/snapshot/src/client.ts index 1742a810f..e6add4e99 100644 --- a/packages/snapshot/src/client.ts +++ b/packages/snapshot/src/client.ts @@ -159,7 +159,7 @@ export class SnapshotClient { options.filepath ||= filepath // resolve and read the raw snapshot file rawSnapshot.file = await snapshotState.environment.resolveRawPath(filepath, rawSnapshot.file) - rawSnapshot.content = await snapshotState.environment.readSnapshotFile(rawSnapshot.file) || undefined + rawSnapshot.content = await snapshotState.environment.readSnapshotFile(rawSnapshot.file) ?? undefined } return this.assert(options) diff --git a/test/core/test/fixtures/snapshot-empty.txt b/test/core/test/fixtures/snapshot-empty.txt new file mode 100644 index 000000000..e69de29bb diff --git a/test/core/test/snapshot-file.test.ts b/test/core/test/snapshot-file.test.ts index c1eb01cf7..28c65b014 100644 --- a/test/core/test/snapshot-file.test.ts +++ b/test/core/test/snapshot-file.test.ts @@ -18,3 +18,7 @@ describe('snapshots', () => { }) } }) + +test('handle empty file', () => { + expect('').toMatchFileSnapshot('./fixtures/snapshot-empty.txt') +}) -- 2.51.2