Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add coverage for cli.ts #178

Merged
merged 2 commits into from
Feb 18, 2024
Merged
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
Binary file added .DS_Store
Binary file not shown.
20 changes: 20 additions & 0 deletions src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it, vi } from "vitest";

import { shouldSemanticReleaseCLI } from "./cli.js";
import { shouldSemanticRelease } from "./shouldSemanticRelease.js";

vi.mock("./shouldSemanticRelease");

describe("cli", () => {
it("calls shouldSemanticRelease without verbose when --verbose is not provided", async () => {
await shouldSemanticReleaseCLI([]);

expect(shouldSemanticRelease).toHaveBeenCalledWith({ verbose: false });
});

it("calls shouldSemanticRelease with verbose when --verbose is provided", async () => {
await shouldSemanticReleaseCLI(["--verbose"]);

expect(shouldSemanticRelease).toHaveBeenCalledWith({ verbose: true });
});
});
37 changes: 37 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it, vi } from "vitest";

import { execOrThrow } from "./utils.js";

const mockExec = vi.fn();

vi.mock("node:child_process", () => ({
default: {
get exec() {
return mockExec;
},
},
}));

describe("execOrThrow", () => {
it("throws an error when exec prints to stderr", async () => {
mockExec.mockImplementation((command, callback) => {
callback(null, { stderr: "Oh no!" });
});

await expect(
async () => await execOrThrow("ls"),
).rejects.toMatchInlineSnapshot(`[Error: Oh no!]`);
});

it("returns stdout when exec doesn't print to stderr", async () => {
const stdout = "abc123";

mockExec.mockImplementation((command, callback) => {
callback(null, { stdout });
});

const actual = await execOrThrow("ls");

expect(actual).toEqual(stdout);
});
});
3 changes: 1 addition & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import cp from "node:child_process";
import { promisify } from "node:util";

const exec = promisify(cp.exec);

export async function execOrThrow(command: string) {
const exec = promisify(cp.exec);
const { stderr, stdout } = await exec(command);

if (stderr) {