Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion testing/unstable_snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
);
}
});

/**
Expand Down
46 changes: 46 additions & 0 deletions testing/unstable_snapshot_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>({
serializer: stripAnsiCode,
Expand Down
Loading