zod-opts
Version:
node.js CLI option parser / validator using Zod
119 lines (116 loc) • 4.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const zod_1 = require("zod");
const command_1 = require("../src/command");
const parser_1 = require("../src/parser");
const test_util_1 = require("./test_util");
describe("getHelp", () => {
test("return help string", () => {
const expectedHelp = `Usage: scriptA [options] <pos1>
desc
Arguments:
pos1 desc5 [required]
Options:
-h, --help Show help
-V, --version Show version
--opt1 <string> desc1 [required]
`;
const help = (0, parser_1.parser)()
.name("scriptA")
.version("1.0.0")
.description("desc")
.options({
opt1: { type: zod_1.z.string().describe("desc1") },
})
.args([
{
name: "pos1",
description: "desc5",
type: zod_1.z.string(),
},
])
.getHelp();
expect(help).toEqual(expectedHelp);
});
});
describe("showHelp()", () => {
test("displays help information when showHelp() is called", () => {
const mockedConsoleLog = (0, test_util_1.mockConsole)("log");
(0, parser_1.parser)().name("scriptA").version("1.0.0").description("desc").showHelp();
const logText = mockedConsoleLog.mock.calls.flat().join("\n");
expect(logText).toMatch(/Usage: scriptA \[options\]/);
});
});
describe("options()", () => {
describe("runtime error", () => {
test("throws runtime exception when an invalid option name is provided", () => {
expect(() => {
(0, parser_1.parser)()
.options({
"--opt1": { type: zod_1.z.string() },
})
.parse([]);
}).toThrow("Invalid option name. Supported pattern is /^[A-Za-z0-9_]+[A-Za-z0-9_-]*$/: --opt1");
});
test("throws runtime exception when invalid alias name is used", () => {
expect(() => {
(0, parser_1.parser)()
.options({
opt1: { type: zod_1.z.string(), alias: "-a" },
})
.parse([]);
}).toThrow("Invalid option alias. Alias must match the pattern /^[A-Za-z0-9_]+$/: -a");
});
});
});
describe("args()", () => {
describe("runtime error", () => {
test("throws runtime exception when invalid argument name is provided", () => {
expect(() => {
(0, parser_1.parser)()
.args([
{
name: "--arg1",
type: zod_1.z.string(),
},
])
.parse([]);
}).toThrow("Invalid argument name provided. Supported pattern is /^[A-Za-z0-9_]+[A-Za-z0-9_-]*$/: --arg1");
});
test("throws runtime exception when duplicate argument name is provided", () => {
expect(() => {
(0, parser_1.parser)()
.args([
{ name: "arg1", type: zod_1.z.string() },
{ name: "arg1", type: zod_1.z.string() },
])
.parse([]);
}).toThrow("Duplicate positional argument name: arg1");
});
test("throws runtime exception when option name and argument name are the same", () => {
expect(() => {
(0, parser_1.parser)()
.options({ opt1: { type: zod_1.z.string() } })
.args([{ name: "opt1", type: zod_1.z.string() }])
.parse([]);
}).toThrowError("Duplicate option name with positional argument name: opt1");
});
});
});
describe("subcommand()", () => {
test("Adding subcommand to parser with args() throws an error.", () => {
expect(() => {
(0, parser_1.parser)()
.args([{ name: "arg1", type: zod_1.z.string() }])
.subcommand((0, command_1.command)("cmd1").args([{ name: "arg1", type: zod_1.z.string() }]));
}).toThrowError("Cannot add subcommand to parser with args().");
});
test("Subcommand cannot be added to parser with options().", () => {
expect(() => {
(0, parser_1.parser)()
.options({ opt1: { type: zod_1.z.string() } })
.subcommand((0, command_1.command)("cmd1").args([{ name: "arg1", type: zod_1.z.string() }]));
}).toThrowError("Subcommand cannot be added to parser with options().");
});
});
//# sourceMappingURL=parser.test.js.map