Skip to content

Commit

Permalink
assert CLI logs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Nov 15, 2024
1 parent fc6e21c commit 595eddb
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 34 deletions.
113 changes: 80 additions & 33 deletions packages/docusaurus/src/commands/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@ async function testCommand(args: string[]) {
const siteDir = path.resolve(__dirname, '__fixtures__', 'site');

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

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

try {
cli.parse(cliArgs);
await cli.parseAsync(cliArgs);
} catch (e) {
if (e !== ExitOverrideError) {
throw e;
}
}

jest.restoreAllMocks();

return {
exit,
stdout,
Expand All @@ -62,30 +73,62 @@ describe('CLI', () => {
const result = await testCommand(['--help']);

expect(result).toMatchInlineSnapshot(`
{
"exit": {
"code": "commander.helpDisplayed",
"exitCode": 0,
},
"stderr": "todo",
"stdout": "todo",
}
`);
{
"exit": {
"code": "commander.helpDisplayed",
"exitCode": 0,
},
"stderr": "",
"stdout": "Usage: docusaurus <command> [options]
Options:
-V, --version output the version number
-h, --help display help for command
Commands:
build [options] [siteDir] Build website.
swizzle [options] [themeName] [componentName] [siteDir] Wraps or ejects the original theme files into website folder for customization.
deploy [options] [siteDir] Deploy website to GitHub pages.
start [options] [siteDir] Start the development server.
serve [options] [siteDir] Serve website locally.
clear [siteDir] Remove build artifacts.
write-translations [options] [siteDir] Extract required translations of your site.
write-heading-ids [options] [siteDir] [files...] Generate heading ids in Markdown content.
cliPlugin:test [options] Run test cli command
",
}
`);
});

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

expect(result).toMatchInlineSnapshot(`
{
"exit": {
"code": "commander.helpDisplayed",
"exitCode": 0,
},
"stderr": "todo",
"stdout": "todo",
}
`);
{
"exit": {
"code": "commander.helpDisplayed",
"exitCode": 0,
},
"stderr": "",
"stdout": "Usage: docusaurus <command> [options]
Options:
-V, --version output the version number
-h, --help display help for command
Commands:
build [options] [siteDir] Build website.
swizzle [options] [themeName] [componentName] [siteDir] Wraps or ejects the original theme files into website folder for customization.
deploy [options] [siteDir] Deploy website to GitHub pages.
start [options] [siteDir] Start the development server.
serve [options] [siteDir] Serve website locally.
clear [siteDir] Remove build artifacts.
write-translations [options] [siteDir] Extract required translations of your site.
write-heading-ids [options] [siteDir] [files...] Generate heading ids in Markdown content.
cliPlugin:test [options] Run test cli command
",
}
`);
});
});

Expand All @@ -99,8 +142,9 @@ describe('CLI', () => {
"code": "commander.version",
"exitCode": 0,
},
"stderr": "todo",
"stdout": "todo",
"stderr": "",
"stdout": "<CURRENT_VERSION>
",
}
`);
});
Expand All @@ -114,8 +158,9 @@ describe('CLI', () => {
"code": "commander.version",
"exitCode": 0,
},
"stderr": "todo",
"stdout": "todo",
"stderr": "",
"stdout": "<CURRENT_VERSION>
",
}
`);
});
Expand Down Expand Up @@ -146,8 +191,8 @@ describe('CLI', () => {
"code": "commander.unknownOption",
"exitCode": 1,
},
"stderr": "todo",
"stdout": "todo",
"stderr": "error: unknown option '--unknown'",
"stdout": "",
}
`);
});
Expand All @@ -160,8 +205,9 @@ describe('CLI', () => {
expect(result).toMatchInlineSnapshot(`
{
"exit": undefined,
"stderr": "todo",
"stdout": "todo",
"stderr": "",
"stdout": "TEST ACTION
",
}
`);
});
Expand All @@ -171,8 +217,9 @@ describe('CLI', () => {
expect(result).toMatchInlineSnapshot(`
{
"exit": undefined,
"stderr": "todo",
"stdout": "todo",
"stderr": "",
"stdout": "TEST ACTION
",
}
`);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/src/commands/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function runCLI(cliArgs: CLIArgs): Promise<void> {
siteDir: DEFAULT_SITE_DIR,
config: DEFAULT_CONFIG,
});
program.parse(cliArgs);
await program.parseAsync(cliArgs);
}

export async function createCLIProgram({
Expand Down

0 comments on commit 595eddb

Please sign in to comment.