Skip to content
Myr

command.alias.test.js

This file

Path

tests/command.alias.test.js

124 lines · 3.8 KiB · JavaScript

Last changed by

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

History of this file

Take it

The walk stopped after 200 commits: lines older than 669190d point at it.

Merge branch 'master' into developJohn Geebefore adoption669190d

lines 1–75why, one step back
1const commander = require('../');
23// Running alias commands is tested in command.executableSubcommand.lookup.test.js4// Test various other behaviours for .alias56test('when command has alias then appears in help', () => {7 const program = new commander.Command();8 program9 .command('info [thing]')10 .alias('i');11 const helpInformation = program.helpInformation();12 expect(helpInformation).toMatch('info|i');13});1415test('when command has aliases added separately then only first appears in help', () => {16 const program = new commander.Command();17 program18 .command('list [thing]')19 .alias('ls')20 .alias('dir');21 const helpInformation = program.helpInformation();22 expect(helpInformation).toMatch('list|ls ');23});2425test('when command has aliases then only first appears in help', () => {26 const program = new commander.Command();27 program28 .command('list [thing]')29 .aliases(['ls', 'dir']);30 const helpInformation = program.helpInformation();31 expect(helpInformation).toMatch('list|ls ');32});3334test('when command name = alias then error', () => {35 const program = new commander.Command();36 expect(() => {37 program38 .command('fail')39 .alias('fail');40 }).toThrow("Command alias can't be the same as its name");41});4243test('when use alias then action handler called', () => {44 const program = new commander.Command();45 const actionMock = jest.fn();46 program47 .command('list')48 .alias('ls')49 .action(actionMock);50 program.parse(['ls'], { from: 'user' });51 expect(actionMock).toHaveBeenCalled();52});5354test('when use second alias added separately then action handler called', () => {55 const program = new commander.Command();56 const actionMock = jest.fn();57 program58 .command('list')59 .alias('ls')60 .alias('dir')61 .action(actionMock);62 program.parse(['dir'], { from: 'user' });63 expect(actionMock).toHaveBeenCalled();64});6566test('when use second of aliases then action handler called', () => {67 const program = new commander.Command();68 const actionMock = jest.fn();69 program70 .command('list')71 .aliases(['ls', 'dir'])72 .action(actionMock);73 program.parse(['dir'], { from: 'user' });74 expect(actionMock).toHaveBeenCalled();75});

Merge branch 'develop' into release/7.xJohn Geebefore adoptiond90ef12

lines 76–89why, one step back
76
77test('when set alias then can get alias', () => {78 const program = new commander.Command();79 const alias = 'abcde';80 program.alias(alias);81 expect(program.alias()).toEqual(alias);82});8384test('when set aliases then can get aliases', () => {85 const program = new commander.Command();86 const aliases = ['a', 'b'];87 program.aliases(aliases);88 expect(program.aliases()).toEqual(aliases);89});

Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2

lines 90–98why, one step back
90
91test('when set alias on executable then can get alias', () => {92 const program = new commander.Command();93 const alias = 'abcde';94 program95 .command('external', 'external command')96 .alias(alias);97 expect(program.commands[0].alias()).toEqual(alias);98});

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 99–124why, one step back
99
100describe('aliases parameter is treated as readonly, per TypeScript declaration', () => {101 test('when aliases called then parameter does not change', () => {102 // Unlikely this could break, but check the API we are declaring in TypeScript.103 const original = ['b', 'bld'];104 const param = original.slice();105 new commander.Command('build').aliases(param);106 expect(param).toEqual(original);107 });108109 test('when aliases called and aliases later changed then parameter does not change', () => {110 const original = ['b', 'bld'];111 const param = original.slice();112 const cmd = new commander.Command('build').aliases(param);113 cmd.alias('BBB');114 expect(param).toEqual(original);115 });116117 test('when aliases called and parameter later changed then aliases does not change', () => {118 const original = ['b', 'bld'];119 const param = original.slice();120 const cmd = new commander.Command('build').aliases(param);121 param.length = 0;122 expect(cmd.aliases()).toEqual(original);123 });124});