Skip to content

Commit 241b948

Browse files
authored
test: add test for subcommand (#191)
1 parent 67b5213 commit 241b948

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

test/main.test.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
runMain,
88
showUsage,
99
} from "../src";
10-
import * as mainModule from "../src/main";
1110
import * as commandModule from "../src/command";
1211

1312
describe("runMain", () => {
@@ -120,6 +119,70 @@ describe("runMain", () => {
120119
});
121120
});
122121

122+
describe("sub command", () => {
123+
it("runs the sub command", async () => {
124+
const setupMock = vi.fn();
125+
const runMock = vi.fn();
126+
const cleanupMock = vi.fn();
127+
128+
const command = defineCommand({
129+
subCommands: {
130+
foo: {
131+
args: {
132+
bar: {
133+
type: "positional",
134+
},
135+
},
136+
setup: async ({ args }) => {
137+
setupMock(args.bar);
138+
},
139+
cleanup: async ({ args }) => {
140+
cleanupMock(args.bar);
141+
},
142+
run: async ({ args }) => {
143+
runMock(args.bar);
144+
},
145+
},
146+
},
147+
});
148+
149+
await runMain(command, { rawArgs: ["foo", "bar"] });
150+
151+
expect(setupMock).toHaveBeenCalledOnce();
152+
expect(setupMock).toHaveBeenCalledWith("bar");
153+
expect(runMock).toHaveBeenCalledOnce();
154+
expect(runMock).toHaveBeenCalledWith("bar");
155+
expect(cleanupMock).toHaveBeenCalledOnce();
156+
expect(cleanupMock).toHaveBeenCalledWith("bar");
157+
});
158+
});
159+
160+
describe("resolveSubCommand", () => {
161+
it("resolves the sub command", async () => {
162+
const command = defineCommand({
163+
subCommands: {
164+
foo: {
165+
args: {
166+
bar: {
167+
type: "positional",
168+
},
169+
},
170+
},
171+
},
172+
});
173+
174+
const [subCommand] = await commandModule.resolveSubCommand(command, [
175+
"foo",
176+
"bar",
177+
]);
178+
179+
expect(subCommand).toBeDefined();
180+
expect(subCommand.args).toEqual({
181+
bar: { type: "positional" },
182+
});
183+
});
184+
});
185+
123186
describe("createMain", () => {
124187
it("creates and returns a function", () => {
125188
const main = createMain(defineCommand({}));

0 commit comments

Comments
 (0)