options.version.test.js
This file
Path
tests/options.version.test.js
183 lines · 6.0 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184import * as commander from '../index.js';import { test, describe } from 'node:test';import assert from 'node:assert/strict';
// Test .version. Using exitOverride to check behaviour (instead of mocking process.exit).
describe('Command.version()', () => { test('when no .version and specify --version then unknown option error', () => { const program = new commander.Command(); program.exitOverride().configureOutput({ writeErr: () => {} });
assert.throws( () => { program.parse(['node', 'test', '--version']); }, { code: 'commander.unknownOption' }, ); });
test('when no .version then helpInformation does not include version', () => { const program = new commander.Command();
const helpInformation = program.helpInformation();
assert.equal(helpInformation.includes('Usage'), true); assert.equal(helpInformation.includes('version'), false); });
test('when specify default short flag then display version', (t) => { const myVersion = '1.2.3'; const writeMock = t.mock.fn(); const program = new commander.Command(); program .exitOverride() .configureOutput({ writeOut: writeMock }) .version(myVersion);
assert.throws(() => { program.parse(['node', 'test', '-V']); }, new RegExp(myVersion)); const callArgs = writeMock.mock.calls[0].arguments; assert.equal(callArgs[0], `${myVersion}\n`); });
test('when specify default long flag then display version', (t) => { const myVersion = '1.2.3'; const writeMock = t.mock.fn(); const program = new commander.Command(); program .exitOverride() .configureOutput({ writeOut: writeMock }) .version(myVersion);
assert.throws(() => { program.parse(['node', 'test', '--version']); }, new RegExp(myVersion)); const callArgs = writeMock.mock.calls[0].arguments; assert.equal(callArgs[0], `${myVersion}\n`); });
test('when default .version then helpInformation includes default version help', () => { const myVersion = '1.2.3'; const program = new commander.Command(); program.version(myVersion);
const helpInformation = program.helpInformation();
assert.equal(helpInformation.includes('-V, --version'), true); assert.equal(helpInformation.includes('output the version number'), true); });
test('when specify custom short flag then display version', (t) => { const myVersion = '1.2.3'; const writeMock = t.mock.fn(); const program = new commander.Command(); program .exitOverride() .configureOutput({ writeOut: writeMock }) .version(myVersion, '-r, --revision');
assert.throws(() => { program.parse(['node', 'test', '-r']); }, new RegExp(myVersion)); const callArgs = writeMock.mock.calls[0].arguments; assert.equal(callArgs[0], `${myVersion}\n`); });
test('when specify just custom short flag then display version', (t) => { const myVersion = '1.2.3'; const writeMock = t.mock.fn(); const program = new commander.Command(); program .exitOverride() .configureOutput({ writeOut: writeMock }) .version(myVersion, '-r');
assert.throws(() => { program.parse(['node', 'test', '-r']); }, new RegExp(myVersion)); const callArgs = writeMock.mock.calls[0].arguments; assert.equal(callArgs[0], `${myVersion}\n`); });
test('when specify custom long flag then display version', (t) => { const myVersion = '1.2.3'; const writeMock = t.mock.fn(); const program = new commander.Command(); program .exitOverride() .configureOutput({ writeOut: writeMock }) .version(myVersion, '-r, --revision');
assert.throws(() => { program.parse(['node', 'test', '--revision']); }, new RegExp(myVersion)); const callArgs = writeMock.mock.calls[0].arguments; assert.equal(callArgs[0], `${myVersion}\n`); });
test('when specify just custom long flag then display version', (t) => { const myVersion = '1.2.3'; const writeMock = t.mock.fn(); const program = new commander.Command(); program .exitOverride() .configureOutput({ writeOut: writeMock }) .version(myVersion, '--revision');
assert.throws(() => { program.parse(['node', 'test', '--revision']); }, new RegExp(myVersion)); const callArgs = writeMock.mock.calls[0].arguments; assert.equal(callArgs[0], `${myVersion}\n`); });
test('when custom .version then helpInformation includes custom version help', () => { const myVersion = '1.2.3'; const myVersionFlags = '-r, --revision'; const myVersionDescription = 'custom description'; const program = new commander.Command(); program.version(myVersion, myVersionFlags, myVersionDescription);
const helpInformation = program.helpInformation();
assert.equal(helpInformation.includes(myVersionFlags), true); assert.equal(helpInformation.includes(myVersionDescription), true); });
test('when have .version+version and specify version then command called', (t) => { const actionMock = t.mock.fn(); const program = new commander.Command(); program.version('1.2.3').command('version').action(actionMock);
program.parse(['node', 'test', 'version']);
assert.equal(actionMock.mock.callCount(), 1); });
test('when have .version+version and specify --version then version displayed', (t) => { const myVersion = '1.2.3'; const writeMock = t.mock.fn(); const program = new commander.Command(); program .exitOverride() .configureOutput({ writeOut: writeMock }) .version(myVersion) .command('version') .action(() => {});
assert.throws(() => { program.parse(['node', 'test', '--version']); }, new RegExp(myVersion)); const callArgs = writeMock.mock.calls[0].arguments; assert.equal(callArgs[0], `${myVersion}\n`); });
test('when specify version then can get version', () => { const myVersion = '1.2.3'; const program = new commander.Command(); program.version(myVersion); assert.equal(program.version(), myVersion); });});