Skip to content

Commit f63bd18

Browse files
committed
test(cli): add unit test for help and version commands
1 parent 4246081 commit f63bd18

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

@commitlint/cli/src/cli.test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,57 @@ test('should work with relative formatter path', async () => {
428428
expect(actual.exitCode).toBe(0);
429429
});
430430

431+
test('should print help', async () => {
432+
const cwd = await gitBootstrap('fixtures/default');
433+
const actual = await cli(['--help'], {cwd})();
434+
expect(escapeCwd(actual.stdout, cwd)).toMatchInlineSnapshot(`
435+
"@commitlint/[email protected] - Lint your commit messages
436+
437+
[input] reads from stdin if --edit, --env, --from and --to are omitted
438+
439+
Options:
440+
--color, -c toggle colored output [boolean] [default: true]
441+
--config, -g path to the config file [string]
442+
--cwd, -d directory to execute in
443+
[string] [default: \\"__CWD__\\"]
444+
--edit, -e read last commit message from the specified file or
445+
fallbacks to ./.git/COMMIT_EDITMSG
446+
[string] [default: false]
447+
--env, -E check message in the file at path given by environment
448+
variable value [string]
449+
--extends, -x array of shareable configurations to extend [array]
450+
--help-url, -H help url in error message [string]
451+
--from, -f lower end of the commit range to lint; applies if
452+
edit=false [string]
453+
--format, -o output format of the results [string]
454+
--parser-preset, -p configuration preset to use for
455+
conventional-commits-parser [string]
456+
--quiet, -q toggle console output [boolean] [default: false]
457+
--to, -t upper end of the commit range to lint; applies if
458+
edit=false [string]
459+
--verbose, -V enable verbose output for reports without problems
460+
[boolean]
461+
-v, --version display version information [boolean]
462+
-h, --help Show help [boolean]"
463+
`);
464+
});
465+
466+
test('should print version', async () => {
467+
const cwd = await gitBootstrap('fixtures/default');
468+
const pkg = require('../package.json');
469+
const actual = await cli(['--version'], {cwd})();
470+
expect(actual.stdout).toMatch(`@commitlint/cli@${pkg.version}`);
471+
});
472+
431473
async function writePkg(payload: unknown, options: TestOptions) {
432474
const pkgPath = path.join(options.cwd, 'package.json');
433475
const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
434476
const result = merge(pkg, payload);
435477
await fs.writeFile(pkgPath, JSON.stringify(result, null, ' '));
436478
}
479+
480+
function escapeCwd(value: string, cwd: string): string {
481+
return value
482+
.replace(cwd, '__CWD__')
483+
.replace(/\n[ ]+(.+__CWD__)/, `\n${' '.repeat(cwd.length - 7)}$1`);
484+
}

@commitlint/cli/src/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const cli = yargs
6060
'help-url': {
6161
alias: 'H',
6262
type: 'string',
63-
description: 'helpurl in error message'
63+
description: 'help url in error message'
6464
},
6565
from: {
6666
alias: 'f',

0 commit comments

Comments
 (0)