Skip to content

Commit e765634

Browse files
Copilotfuxingloh
andcommitted
chore: capture exit code in tests
Co-authored-by: fuxingloh <4266087+fuxingloh@users.noreply.github.com>
1 parent 7e97ea8 commit e765634

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

packages/use-agently/src/bin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ installTelemetry(cli);
1111
function resolveOutputFormat(): "tui" | "json" {
1212
try {
1313
return getOutputFormat(cli);
14-
} catch {
15-
// If option parsing fails, still show the underlying CLI error without double-logging here.
14+
} catch (err) {
15+
console.warn("Falling back to TUI output after failing to resolve --output option.", err);
1616
return "tui";
1717
}
1818
}

packages/use-agently/src/errors.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ describe("handleCliError", () => {
55
let exitSpy: ReturnType<typeof spyOn>;
66
let errorSpy: ReturnType<typeof spyOn>;
77
let originalIsTTY: boolean | undefined;
8+
let lastExitCode: number | undefined;
89

910
beforeEach(() => {
1011
originalIsTTY = process.stderr.isTTY;
12+
lastExitCode = undefined;
1113
exitSpy = spyOn(process, "exit").mockImplementation((code?: number) => {
14+
lastExitCode = code;
1215
// Bubble a sentinel error so tests can assert the exit while preventing the process from terminating.
1316
throw new Error("process.exit");
1417
});
@@ -24,6 +27,7 @@ describe("handleCliError", () => {
2427
test("prints json error when format is json", () => {
2528
expect(() => handleCliError(new Error("boom"), "json")).toThrow("process.exit");
2629
expect(exitSpy).toHaveBeenCalledWith(1);
30+
expect(lastExitCode).toBe(1);
2731
expect(errorSpy).toHaveBeenCalledWith(JSON.stringify({ error: { message: "boom" } }));
2832
});
2933

@@ -32,6 +36,7 @@ describe("handleCliError", () => {
3236

3337
expect(() => handleCliError(new Error("oops"), "tui")).toThrow("process.exit");
3438
expect(exitSpy).toHaveBeenCalledWith(1);
39+
expect(lastExitCode).toBe(1);
3540
expect(errorSpy).toHaveBeenCalledWith("Error: oops");
3641
});
3742

@@ -40,6 +45,7 @@ describe("handleCliError", () => {
4045

4146
expect(() => handleCliError(new Error("boxed"), "tui")).toThrow("process.exit");
4247
expect(exitSpy).toHaveBeenCalledWith(1);
48+
expect(lastExitCode).toBe(1);
4349
const output = errorSpy.mock.calls.map((call) => call[0]).join("");
4450
expect(output).toContain("boxed");
4551
expect(output).toContain("Error");

packages/use-agently/src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { OutputFormat } from "./output.js";
33

44
function isProcessExitError(err: unknown): err is Error {
55
// Commander calls process.exit() internally; in tests we mock it to throw an error
6-
// with this message so the test runner can intercept the exit.
6+
// with this message so the test runner can intercept the exit without double-formatting.
77
return err instanceof Error && err.message === "process.exit";
88
}
99

0 commit comments

Comments
 (0)