import { CliProgramDefinition, CliCommandDefinition, ParsedCliCommand, CliParsedCommandHandler, CliCommandHandler } from '../src'; import { parseCommandLine, createProgram } from './helpers/utils'; describe('Global Handler', () => { const buildCmdHandler: CliCommandHandler = (args, options) => { console.log('buildCmdHandler called') } const buildCmdHandlerMock = jest.fn(buildCmdHandler) const buildCmdDef: CliCommandDefinition = { name: 'build', description: 'my description', handler: buildCmdHandlerMock, arguments: [ { name: 'src', description: 'Source file' }, { name: 'dest', description: 'Desination File', isOptional: true, default: 'Default Value' }, ], options: [ { name: 'output', flag: '-o', description: 'output description', }, ], transformArguments: (args) => { return { ...args, ...{ newval: 1 }} }, transformOptions: (options) => { //return { ...options, ...{ newval: 1 }} options['newval'] = 1; return options; } }; const globalHandler: CliParsedCommandHandler = (parsed: ParsedCliCommand) => {} const globalHandlerMock = jest.fn(globalHandler); const def = createProgram('test1', buildCmdDef); const p1 = parseCommandLine('build a b', def, '1.0.0', { globalHandler: globalHandlerMock}); it('Run Command with global handler', async () => { jest.resetAllMocks(); await p1.run(); expect(globalHandlerMock).toBeCalledTimes(1); expect(buildCmdHandlerMock).toBeCalledTimes(0); }) const p2 = parseCommandLine('build a b', def, '1.0.0'); it('Run Command with def command handler', async () => { jest.resetAllMocks(); await p2.run(); expect(globalHandlerMock).toBeCalledTimes(0); expect(buildCmdHandlerMock).toBeCalledTimes(1); }) })