Skip to content

Commit 0a10e18

Browse files
committed
tests
1 parent f02de06 commit 0a10e18

File tree

2 files changed

+100
-60
lines changed

2 files changed

+100
-60
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`cli completion tests for citty > cli option completion tests > should complete option for partial input '{ partial: '--p', expected: '--port' }' 1`] = `
4+
"--port Specify port
5+
"
6+
`;
7+
8+
exports[`cli completion tests for citty > cli option exclusion tests > should not suggest already specified option '{ specified: '--config', shouldNotContain: '--config' }' 1`] = `
9+
"--mode Set env mode
10+
--logLevel info | warn | error | silent
11+
:4
12+
"
13+
`;
14+
15+
exports[`cli completion tests for citty > cli option exclusion tests > should not suggest already specified option '{ specified: '--port', shouldNotContain: '--port' }' 1`] = `
16+
"--mode Set env mode
17+
--logLevel info | warn | error | silent
18+
:4
19+
"
20+
`;
21+
22+
exports[`cli completion tests for citty > cli option value handling > should handle conflicting options appropriately 1`] = `
23+
"--mode Set env mode
24+
--logLevel info | warn | error | silent
25+
:4
26+
"
27+
`;
28+
29+
exports[`cli completion tests for citty > cli option value handling > should handle unknown options with no completions 1`] = `""`;
30+
31+
exports[`cli completion tests for citty > cli option value handling > should resolve config option values correctly 1`] = `
32+
"vite.config.js
33+
vite.config.ts
34+
"
35+
`;
36+
37+
exports[`cli completion tests for citty > cli option value handling > should resolve port value correctly 1`] = `
38+
"3000 Development server port
39+
"
40+
`;
41+
42+
exports[`cli completion tests for citty > edge case completions for end with space > should keep suggesting the --port option if user typed partial but didn't end with space 1`] = `
43+
"--port Specify port
44+
"
45+
`;
46+
47+
exports[`cli completion tests for citty > edge case completions for end with space > should suggest port values if user ends with space after \`--port\` 1`] = `
48+
"3000 Development server port
49+
8080 Alternative port
50+
"
51+
`;
52+
53+
exports[`cli completion tests for citty > edge case completions for end with space > should suggest port values if user typed \`--port=\` and hasn't typed a space or value yet 1`] = `
54+
"3000 Development server port
55+
8080 Alternative port
56+
:4
57+
"
58+
`;
59+
60+
exports[`cli completion tests for citty > positional argument completions > should complete multiple positional arguments when ending with space (vite src/ ./) 1`] = `
61+
"./
62+
src/
63+
"
64+
`;
65+
66+
exports[`cli completion tests for citty > positional argument completions > should complete single positional argument when ending with space (vite src/) 1`] = `
67+
"./
68+
"
69+
`;
70+
71+
exports[`cli completion tests for citty > should complete cli options 1`] = `
72+
"dev
73+
devbuild
74+
"
75+
`;

tests/cli.test.ts

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// TODO: convert to inlineSnapShot
21
import { exec } from "child_process";
32
import { describe, it, expect, test } from "vitest";
43

@@ -14,28 +13,14 @@ function runCommand(command: string): Promise<string> {
1413
});
1514
}
1615

17-
const cliTools = ["cac", "citty"];
16+
const cliTools = ["citty"];
1817

1918
describe.each(cliTools)("cli completion tests for %s", (cliTool) => {
2019
const commandPrefix = `pnpm tsx demo.${cliTool}.ts complete --`;
2120

22-
// it("should complete positional commands", async () => {
23-
// const output = await runCommand(commandPrefix);
24-
// console.log(`[${cliTool}] Command Output:`, output);
25-
// expect(output).toContain("src/");
26-
// expect(output).toContain("./");
27-
// // expect(output).toContain('--base');
28-
// });
29-
3021
it("should complete cli options", async () => {
31-
const output = await runCommand(`${commandPrefix} --`);
32-
console.log(`[${cliTool}] Command Output:`, output);
33-
expect(output).toContain("--port");
34-
expect(output).toContain("--config");
35-
expect(output).toContain("--base");
36-
expect(output).toContain("--logLevel");
37-
expect(output).toContain("--filter");
38-
expect(output).toContain("--mode");
22+
const output = await runCommand(`${commandPrefix}`);
23+
expect(output).toMatchSnapshot();
3924
});
4025

4126
describe("cli option completion tests", () => {
@@ -45,107 +30,87 @@ describe.each(cliTools)("cli completion tests for %s", (cliTool) => {
4530

4631
test.each(optionTests)(
4732
"should complete option for partial input '%s'",
48-
async ({ partial, expected }) => {
49-
const command = `${commandPrefix} ${partial}`;
33+
async ({ partial }) => {
34+
const command = `${commandPrefix} dev ${partial}`;
5035
const output = await runCommand(command);
51-
console.log(`[${cliTool}] Complete ${partial} Output:`, output);
52-
expect(output).toContain(expected);
36+
expect(output).toMatchSnapshot();
5337
}
5438
);
5539
});
5640

5741
describe("cli option exclusion tests", () => {
5842
const alreadySpecifiedTests = [
59-
{ specified: "--port", shouldNotContain: "--port" },
43+
{ specified: "--config", shouldNotContain: "--config" },
6044
];
6145

6246
test.each(alreadySpecifiedTests)(
6347
"should not suggest already specified option '%s'",
64-
async ({ specified, shouldNotContain }) => {
48+
async ({ specified }) => {
6549
const command = `${commandPrefix} ${specified} --`;
6650
const output = await runCommand(command);
67-
console.log(`[${cliTool}] Already Specified ${specified} Output:`, output);
68-
expect(output).not.toContain(shouldNotContain);
69-
// expect(output).toContain("--base");
51+
expect(output).toMatchSnapshot();
7052
}
7153
);
7254
});
7355

7456
describe("cli option value handling", () => {
75-
7657
it("should resolve port value correctly", async () => {
77-
const command = `${commandPrefix} --port 3`;
58+
const command = `${commandPrefix} dev --port=3`;
7859
const output = await runCommand(command);
79-
console.log(`[${cliTool}] Port Value Output:`, output);
80-
expect(output).toContain("3000");
60+
expect(output).toMatchSnapshot();
8161
});
8262

8363
it("should handle conflicting options appropriately", async () => {
84-
const command = `${commandPrefix} --port 3000 --`;
64+
const command = `${commandPrefix} --config vite.config.js --`;
8565
const output = await runCommand(command);
86-
console.log(`[${cliTool}] Conflicting Options Output:`, output);
87-
expect(output).not.toContain("--port");
88-
expect(output).toContain("--config");
89-
// expect(output).toContain("--base");
66+
expect(output).toMatchSnapshot();
9067
});
9168

9269
it("should resolve config option values correctly", async () => {
93-
const command = `${commandPrefix} --port 3000 --config vite.config`;
70+
const command = `${commandPrefix} --config vite.config`;
9471
const output = await runCommand(command);
95-
console.log(`[${cliTool}] Config Option Output:`, output);
96-
expect(output).toContain("vite.config.ts");
97-
expect(output).toContain("vite.config.js");
72+
expect(output).toMatchSnapshot();
9873
});
9974

10075
it("should handle unknown options with no completions", async () => {
10176
const command = `${commandPrefix} --unknownoption`;
10277
const output = await runCommand(command);
103-
console.log(`[${cliTool}] No Completion Available Output:`, output);
104-
expect(output.trim()).toMatch(/^(:\d+)?$/);
78+
expect(output.trim()).toMatchSnapshot();
10579
});
10680
});
10781

10882
describe("edge case completions for end with space", () => {
109-
83+
//TOOD: remove this
11084
it("should suggest port values if user ends with space after `--port`", async () => {
11185
const command = `${commandPrefix} dev --port ""`;
11286
const output = await runCommand(command);
113-
console.log(`[${cliTool}] End With Space (port) Output:`, output);
114-
expect(output).toContain("3000");
87+
expect(output).toMatchSnapshot();
11588
});
11689

11790
it("should keep suggesting the --port option if user typed partial but didn't end with space", async () => {
11891
const command = `${commandPrefix} dev --po`;
11992
const output = await runCommand(command);
120-
console.log(`[${cliTool}] Partial Option (no space) Output:`, output);
121-
expect(output).toContain("--port");
93+
expect(output).toMatchSnapshot();
12294
});
12395

12496
it("should suggest port values if user typed `--port=` and hasn't typed a space or value yet", async () => {
125-
const command = `${commandPrefix} dev --port ""`;
97+
const command = `${commandPrefix} dev --port=`;
12698
const output = await runCommand(command);
127-
console.log(`[${cliTool}] --port= (no space) Output:`, output);
128-
expect(output).toContain("3000");
99+
expect(output).toMatchSnapshot();
129100
});
130-
131101
});
132102

133103
describe("positional argument completions", () => {
134104
it("should complete single positional argument when ending with space (vite src/)", async () => {
135105
const command = `${commandPrefix} vite src/ ""`;
136106
const output = await runCommand(command);
137-
console.log(`[${cliTool}] Single Positional Output:`, output);
138-
139-
expect(output).toContain("src/");
140-
expect(output).toContain("./");
107+
expect(output).toMatchSnapshot();
141108
});
142109

143110
it("should complete multiple positional arguments when ending with space (vite src/ ./)", async () => {
144-
const command = `${commandPrefix} vite src/ ""`;
111+
const command = `${commandPrefix} vite ""`;
145112
const output = await runCommand(command);
146-
console.log(`[${cliTool}] Multiple Positional Output:`, output);
147-
148-
expect(output).toContain("./");
113+
expect(output).toMatchSnapshot();
149114
});
150115
});
151-
});
116+
});

0 commit comments

Comments
 (0)