Skip to content

Commit 595eddb

Browse files
committed
assert CLI logs tests
1 parent fc6e21c commit 595eddb

File tree

2 files changed

+81
-34
lines changed

2 files changed

+81
-34
lines changed

packages/docusaurus/src/commands/__tests__/cli.test.ts

Lines changed: 80 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ async function testCommand(args: string[]) {
2020
const siteDir = path.resolve(__dirname, '__fixtures__', 'site');
2121

2222
// TODO Docusaurus v4: upgrade Commander
23-
// unfortunately we can't assert console output because current (v5) doesn't
24-
// let us do so easily
23+
// new versions make it easier to intercept logs
2524
// see https://github.com/tj/commander.js#override-exit-and-output-handling
26-
const stdout = 'todo';
27-
const stderr = 'todo';
25+
let stdout = '';
26+
let stderr = '';
27+
jest.spyOn(console, 'log').mockImplementation((msg: string) => {
28+
stdout += msg;
29+
});
30+
// @ts-expect-error: only used with strings
31+
jest.spyOn(process.stdout, 'write').mockImplementation((msg: string) => {
32+
stdout += String(msg);
33+
});
34+
jest.spyOn(console, 'error').mockImplementation((msg: string) => {
35+
stderr += msg;
36+
});
2837

2938
const cli = await createCLIProgram({
3039
cli: new Command() as CommanderStatic,
@@ -41,13 +50,15 @@ async function testCommand(args: string[]) {
4150
});
4251

4352
try {
44-
cli.parse(cliArgs);
53+
await cli.parseAsync(cliArgs);
4554
} catch (e) {
4655
if (e !== ExitOverrideError) {
4756
throw e;
4857
}
4958
}
5059

60+
jest.restoreAllMocks();
61+
5162
return {
5263
exit,
5364
stdout,
@@ -62,30 +73,62 @@ describe('CLI', () => {
6273
const result = await testCommand(['--help']);
6374

6475
expect(result).toMatchInlineSnapshot(`
65-
{
66-
"exit": {
67-
"code": "commander.helpDisplayed",
68-
"exitCode": 0,
69-
},
70-
"stderr": "todo",
71-
"stdout": "todo",
72-
}
73-
`);
76+
{
77+
"exit": {
78+
"code": "commander.helpDisplayed",
79+
"exitCode": 0,
80+
},
81+
"stderr": "",
82+
"stdout": "Usage: docusaurus <command> [options]
83+
84+
Options:
85+
-V, --version output the version number
86+
-h, --help display help for command
87+
88+
Commands:
89+
build [options] [siteDir] Build website.
90+
swizzle [options] [themeName] [componentName] [siteDir] Wraps or ejects the original theme files into website folder for customization.
91+
deploy [options] [siteDir] Deploy website to GitHub pages.
92+
start [options] [siteDir] Start the development server.
93+
serve [options] [siteDir] Serve website locally.
94+
clear [siteDir] Remove build artifacts.
95+
write-translations [options] [siteDir] Extract required translations of your site.
96+
write-heading-ids [options] [siteDir] [files...] Generate heading ids in Markdown content.
97+
cliPlugin:test [options] Run test cli command
98+
",
99+
}
100+
`);
74101
});
75102

76103
it('docusaurus -h', async () => {
77104
const result = await testCommand(['-h']);
78105

79106
expect(result).toMatchInlineSnapshot(`
80-
{
81-
"exit": {
82-
"code": "commander.helpDisplayed",
83-
"exitCode": 0,
84-
},
85-
"stderr": "todo",
86-
"stdout": "todo",
87-
}
88-
`);
107+
{
108+
"exit": {
109+
"code": "commander.helpDisplayed",
110+
"exitCode": 0,
111+
},
112+
"stderr": "",
113+
"stdout": "Usage: docusaurus <command> [options]
114+
115+
Options:
116+
-V, --version output the version number
117+
-h, --help display help for command
118+
119+
Commands:
120+
build [options] [siteDir] Build website.
121+
swizzle [options] [themeName] [componentName] [siteDir] Wraps or ejects the original theme files into website folder for customization.
122+
deploy [options] [siteDir] Deploy website to GitHub pages.
123+
start [options] [siteDir] Start the development server.
124+
serve [options] [siteDir] Serve website locally.
125+
clear [siteDir] Remove build artifacts.
126+
write-translations [options] [siteDir] Extract required translations of your site.
127+
write-heading-ids [options] [siteDir] [files...] Generate heading ids in Markdown content.
128+
cliPlugin:test [options] Run test cli command
129+
",
130+
}
131+
`);
89132
});
90133
});
91134

@@ -99,8 +142,9 @@ describe('CLI', () => {
99142
"code": "commander.version",
100143
"exitCode": 0,
101144
},
102-
"stderr": "todo",
103-
"stdout": "todo",
145+
"stderr": "",
146+
"stdout": "<CURRENT_VERSION>
147+
",
104148
}
105149
`);
106150
});
@@ -114,8 +158,9 @@ describe('CLI', () => {
114158
"code": "commander.version",
115159
"exitCode": 0,
116160
},
117-
"stderr": "todo",
118-
"stdout": "todo",
161+
"stderr": "",
162+
"stdout": "<CURRENT_VERSION>
163+
",
119164
}
120165
`);
121166
});
@@ -146,8 +191,8 @@ describe('CLI', () => {
146191
"code": "commander.unknownOption",
147192
"exitCode": 1,
148193
},
149-
"stderr": "todo",
150-
"stdout": "todo",
194+
"stderr": "error: unknown option '--unknown'",
195+
"stdout": "",
151196
}
152197
`);
153198
});
@@ -160,8 +205,9 @@ describe('CLI', () => {
160205
expect(result).toMatchInlineSnapshot(`
161206
{
162207
"exit": undefined,
163-
"stderr": "todo",
164-
"stdout": "todo",
208+
"stderr": "",
209+
"stdout": "TEST ACTION
210+
",
165211
}
166212
`);
167213
});
@@ -171,8 +217,9 @@ describe('CLI', () => {
171217
expect(result).toMatchInlineSnapshot(`
172218
{
173219
"exit": undefined,
174-
"stderr": "todo",
175-
"stdout": "todo",
220+
"stderr": "",
221+
"stdout": "TEST ACTION
222+
",
176223
}
177224
`);
178225
});

packages/docusaurus/src/commands/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export async function runCLI(cliArgs: CLIArgs): Promise<void> {
6262
siteDir: DEFAULT_SITE_DIR,
6363
config: DEFAULT_CONFIG,
6464
});
65-
program.parse(cliArgs);
65+
await program.parseAsync(cliArgs);
6666
}
6767

6868
export async function createCLIProgram({

0 commit comments

Comments
 (0)