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: main #162

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
130 changes: 130 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { describe, it, expect, vi, afterAll } from "vitest";
import consola from "consola";
import {
createMain,
defineCommand,
renderUsage,
runMain,
showUsage,
} from "../src";
import * as mainModule from "../src/main";

Check warning on line 10 in test/main.test.ts

View workflow job for this annotation

GitHub Actions / autofix

'mainModule' is defined but never used

Check warning on line 10 in test/main.test.ts

View workflow job for this annotation

GitHub Actions / ci

'mainModule' is defined but never used
import * as commandModule from "../src/command";

describe("runMain", () => {
vi.spyOn(process, "exit").mockImplementation(() => 0 as never);

const consoleMock = vi
.spyOn(consola, "log")
.mockImplementation(() => undefined);

const consolaErrorMock = vi
.spyOn(consola, "error")
.mockImplementation(() => undefined);

afterAll(() => {
consoleMock.mockReset();
});

it("shows version with flag `--version`", async () => {
const command = defineCommand({
meta: {
version: "1.0.0",
},
});

await runMain(command, { rawArgs: ["--version"] });

expect(consoleMock).toHaveBeenCalledWith("1.0.0");
});

it("shows version with flag `--version` with meta specified as async function", async () => {
const command = defineCommand({
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject, require-await
meta: async () => Promise.resolve({ version: "1.0.0" }),
});

await runMain(command, { rawArgs: ["--version"] });

expect(consoleMock).toHaveBeenCalledWith("1.0.0");
});

it("shows usage with flag `--version` but without version specified", async () => {
const command = defineCommand({
meta: {
name: "test",
description: "Test command",
},
});

await runMain(command, { rawArgs: ["--version"] });

const usage = await renderUsage(command);
expect(consoleMock).toHaveBeenCalledWith(usage + "\n");
expect(consolaErrorMock).toHaveBeenCalledWith("No version specified");
});

it.each([["--help"], ["-h"]])("shows usage with flag `%s`", async (flag) => {
const command = defineCommand({
meta: {
name: "test",
description: "Test command",
},
});

await runMain(command, { rawArgs: [flag] });

const usage = await renderUsage(command);
expect(consoleMock).toHaveBeenCalledWith(usage + "\n");
});

it.each([["--help"], ["-h"]])(
"can show custom usage with flag `%s`",
async (flag) => {
const command = defineCommand({
meta: {
name: "test",
description: "Test command",
},
});

// eslint-disable-next-line require-await, unicorn/consistent-function-scoping
const customUsage: typeof showUsage = async () => {
consola.log("Custom usage");
};

await runMain(command, { rawArgs: [flag], showUsage: customUsage });

expect(consoleMock).toHaveBeenCalledWith("Custom usage");
},
);

it("runs the command", async () => {
const mockRunCommand = vi.spyOn(commandModule, "runCommand");

const command = defineCommand({});

await runMain(command);

expect(mockRunCommand).toHaveBeenCalledWith(command, { rawArgs: [] });
});

it("runs the command with raw arguments", async () => {
const mockRunCommand = vi.spyOn(commandModule, "runCommand");

const command = defineCommand({});

const rawArgs = ["--foo", "bar"];

await runMain(command, { rawArgs });

expect(mockRunCommand).toHaveBeenCalledWith(command, { rawArgs });
});
});

describe("createMain", () => {
it("creates and returns a function", () => {
const main = createMain(defineCommand({}));

expect(main).toBeInstanceOf(Function);
});
});