From 3b81700b79d5e923aebc09601eb6361e5406ec74 Mon Sep 17 00:00:00 2001 From: minato32 Date: Wed, 24 Jun 2026 13:05:20 +0530 Subject: [PATCH] fix(testing/unstable): report descriptive error when inline snapshot update fails Closes #6746 --- testing/unstable_snapshot.ts | 10 ++++++- testing/unstable_snapshot_test.ts | 46 +++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/testing/unstable_snapshot.ts b/testing/unstable_snapshot.ts index f2be5334e1b2..55e45f7ed931 100644 --- a/testing/unstable_snapshot.ts +++ b/testing/unstable_snapshot.ts @@ -160,7 +160,15 @@ function updateSnapshots() { ); } globalThis.addEventListener("unload", () => { - updateSnapshots(); + try { + updateSnapshots(); + } catch (error) { + const cause = error instanceof Error ? error : new Error(String(error)); + throw new Error( + `assertInlineSnapshot: failed to update snapshots: ${cause.message}`, + { cause }, + ); + } }); /** diff --git a/testing/unstable_snapshot_test.ts b/testing/unstable_snapshot_test.ts index ab3e6e0a1a2f..62fb22904194 100644 --- a/testing/unstable_snapshot_test.ts +++ b/testing/unstable_snapshot_test.ts @@ -184,6 +184,52 @@ Deno.test("format", async () => { } }); +Deno.test( + "assertInlineSnapshot() reports a descriptive error when update fails", + async () => { + if (!LINT_SUPPORTED) return; + + const fileContents = + `import { assertInlineSnapshot } from "${SNAPSHOT_MODULE_URL}"; +Deno.test("update test", () => { + assertInlineSnapshot(1, \`2\`); +});`; + + const tempDir = await Deno.makeTempDir(); + const testFile = join(tempDir, "update_test.ts"); + try { + await Deno.writeTextFile(testFile, fileContents); + + // Missing --allow-run makes the post-update `deno fmt` subprocess fail. + // The error must surface with a descriptive message and a non-zero exit + // code instead of the test runner reporting `error: null`. + const command = new Deno.Command(Deno.execPath(), { + args: [ + "test", + "--no-lock", + "--allow-read", + "--allow-write", + testFile, + "--", + "--update", + ], + }); + const { code, stderr, stdout } = await command.output(); + const output = new TextDecoder().decode(stderr) + + new TextDecoder().decode(stdout); + + assertEquals(code !== 0, true); + assertEquals( + output.includes("assertInlineSnapshot: failed to update snapshots"), + true, + ); + assertEquals(output.includes("error: null"), false); + } finally { + await Deno.remove(tempDir, { recursive: true }); + } + }, +); + Deno.test("createAssertInlineSnapshot()", () => { const assertMonochromeInlineSnapshot = createAssertInlineSnapshot({ serializer: stripAnsiCode,