Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions src/runtime/cli/test_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,7 @@
if this.jest.bail == 1 { "" } else { "s" }
);
Output::flush();
this.write_snapshots_before_bail();
this.write_junit_report_if_needed();
this.write_timings_if_needed();
Global::exit(1);
Expand Down Expand Up @@ -1552,6 +1553,20 @@
Output::print_start_end(bun::start_time(), bun::time::nano_timestamp());
}

/// Snapshots are otherwise only written back when the next file opens its
/// own `.snap` or when the whole run ends, so a bail exit has to flush them
/// itself: the open `.snap` was created (and, under `--update-snapshots`,
/// truncated) when it was opened and would be left empty on disk, and
/// pending inline snapshots would be dropped.

Check warning on line 1560 in src/runtime/cli/test_command.rs

View check run for this annotation

Claude / Claude Code Review

Doc comment on write_snapshots_before_bail could be condensed to one line

The comment-cop bot's "workaround" framing is misleading here — this function *is* the fix, not a workaround, so the approach is fine. That said, per REVIEW.md ("Only comment what the code cannot say. One line.") the 5-line doc comment could be condensed, e.g.: `/// Flush the open `.snap` (created/truncated on open) and pending inline snapshots; both are otherwise only written at next-file-open or run end.`
Comment thread
robobun marked this conversation as resolved.
Resolved
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
pub(crate) fn write_snapshots_before_bail(&mut self) {
if let Err(err) = self.jest.snapshots.write_inline_snapshots() {
Output::err(err, "Failed to write inline snapshots", ());
}
if let Err(err) = self.jest.snapshots.write_snapshot_file() {
Output::err(err, "Failed to write snapshot file", ());
}
}

/// Like the JUnit report, called before every exit path (including bail) so measured durations aren't lost.
pub(crate) fn write_timings_if_needed(&mut self) {
if self.jest.test_options.update_timings
Expand Down Expand Up @@ -3300,6 +3315,7 @@
reporter.jest.bail,
if reporter.jest.bail == 1 { "" } else { "s" }
);
reporter.write_snapshots_before_bail();
reporter.write_junit_report_if_needed();
reporter.write_timings_if_needed();

Expand Down
86 changes: 84 additions & 2 deletions test/js/bun/test/snapshot-tests/new-snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test";
import { describe, expect, test } from "bun:test";
import fs from "fs";
import { bunEnv, bunExe, tmpdirSync } from "harness";
import { bunEnv, bunExe, tempDir, tmpdirSync } from "harness";

test("it will create a snapshot file and directory if they don't exist", () => {
const tempDir = tmpdirSync();
Expand Down Expand Up @@ -28,3 +28,85 @@ test("it will create a snapshot file and directory if they don't exist", () => {
expect(exitCode2).toBe(0);
expect(fs.existsSync(tempDir + "/__snapshots__/new-snapshot.test.ts.snap")).toBe(true);
});

describe("--bail", () => {
// Snapshots are buffered in memory while a file runs and written back later. Bailing out must
// still write them: the .snap file is created (and truncated under -u) as soon as it is opened.
const header = "// Bun Snapshot v1, https://bun.sh/docs/test/snapshots\n";
const snapEntry = (value: string) => "\nexports[`snap 1`] = `" + value + "`;\n";

const snapTest = /*js*/ `
import { test, expect } from "bun:test";
test("snap", () => { expect("value").toMatchSnapshot(); });
`;
const failingTest = /*js*/ `
import { test, expect } from "bun:test";
test("fails", () => { expect(1).toBe(2); });
`;
const snapThenFailingTest = /*js*/ `
import { test, expect } from "bun:test";
test("snap", () => { expect("value").toMatchSnapshot(); });
test("fails", () => { expect(1).toBe(2); });
`;
const inlineSnap = (snapshot: string) => /*js*/ `
import { test, expect } from "bun:test";
test("inline", () => { expect("value").toMatchInlineSnapshot(${snapshot}); });
test("fails", () => { expect(1).toBe(2); });
`;

async function runUntilBail(dir: string, ...args: string[]) {
await using proc = Bun.spawn({
cmd: [bunExe(), "test", "--bail", ...args],
cwd: String(dir),
env: {
...bunEnv,
CI: "false",
// Bailing on a failed test exits the process without tearing the VM down, so the
// test runner's JSC-owned objects show up as leaks. These tests cover what reaches
// disk before that exit, so keep ASAN on but leave LSAN off for the child.
ASAN_OPTIONS: [bunEnv.ASAN_OPTIONS, "detect_leaks=0"].filter(Boolean).join(":"),
},
stdout: "pipe",
stderr: "pipe",
});
const [, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toContain("Bailed out after 1 failure");
expect(exitCode).toBe(1);
}

test.concurrent("writes the snapshots recorded in the file that bailed", async () => {
using dir = tempDir("snapshot-bail", { "a.test.ts": snapThenFailingTest });
await runUntilBail(dir, "./a.test.ts");
expect(fs.readFileSync(`${dir}/__snapshots__/a.test.ts.snap`, "utf8")).toBe(header + snapEntry('"value"'));
});

test.concurrent("--update-snapshots writes the updated snapshots instead of leaving the file empty", async () => {
using dir = tempDir("snapshot-bail-update", {
"a.test.ts": snapThenFailingTest,
"__snapshots__": { "a.test.ts.snap": header + snapEntry('"stale"') },
});
await runUntilBail(dir, "--update-snapshots", "./a.test.ts");
expect(fs.readFileSync(`${dir}/__snapshots__/a.test.ts.snap`, "utf8")).toBe(header + snapEntry('"value"'));
});

test.concurrent("writes the snapshots of an earlier file when a later file's test fails", async () => {
using dir = tempDir("snapshot-bail-earlier-file", { "a.test.ts": snapTest, "b.test.ts": failingTest });
await runUntilBail(dir, "./a.test.ts", "./b.test.ts");
expect(fs.readFileSync(`${dir}/__snapshots__/a.test.ts.snap`, "utf8")).toBe(header + snapEntry('"value"'));
});
Comment thread
robobun marked this conversation as resolved.
Resolved

test.concurrent("writes the snapshots of an earlier file when a later file fails to load", async () => {
using dir = tempDir("snapshot-bail-load-error", {
"a.test.ts": snapTest,
"b.test.ts": `throw new Error("failed to load");`,
});
await runUntilBail(dir, "./a.test.ts", "./b.test.ts");
expect(fs.readFileSync(`${dir}/__snapshots__/a.test.ts.snap`, "utf8")).toBe(header + snapEntry('"value"'));
});

test.concurrent("writes pending inline snapshots", async () => {
using dir = tempDir("snapshot-bail-inline", { "a.test.ts": inlineSnap("") });
await runUntilBail(dir, "./a.test.ts");
expect(fs.readFileSync(`${dir}/a.test.ts`, "utf8")).toBe(inlineSnap('`"value"`'));
});
});
Loading