Skip to content

Commit

Permalink
Further test throw
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Sep 6, 2023
1 parent 02fc885 commit 7afe5d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/__tests__/cli/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,31 @@ test("main random throw", async (t) => {
t.true(log[0][1].includes("index.test"));
});

test("main random throw no stack", async (t) => {
t.plan(4);

const log: [fn: "log" | "error", message: string][] = [];

const error = new Error("test error");
delete error.stack;

const dMock = mock<D>(t)
.setup((d) => d.argv)
.throws(error)
.setup((d) => d.error)
.returns((message) => log.push(["error", message.replace(/\r/g, "")]))
.setup((d) => d.setExitCode)
.returns((code) => {
t.is(code, 1);
});

await main(dMock.object());

t.is(log[0][0], "error");
t.true(log[0][1].includes("test error"));
t.false(log[0][1].includes("index.test"));
});

test("main random throw primitive", async (t) => {
t.plan(3);

Expand Down
4 changes: 2 additions & 2 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export async function main(d: D) {
}
if (e instanceof UserError) {
d.error(`${pc.red("Error")}: ${e.message}`);
} else if (util.types.isNativeError(e)) {
d.error(e.stack || e.message);
} else if (util.types.isNativeError(e) && e.stack) {
d.error(e.stack);
} else {
d.error(`${e}`);
}
Expand Down

0 comments on commit 7afe5d8

Please sign in to comment.