Skip to content

Commit

Permalink
test(yargs): fix onFinishCommand test with promise handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Grishin committed Nov 20, 2019
1 parent 2cfcae6 commit f1d41ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
9 changes: 5 additions & 4 deletions lib/command.js
Expand Up @@ -240,12 +240,13 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
handlerResult = commandHandler.handler(innerArgv)
}

const handlerFinishCommand = yargs.getHandlerFinishCommand()
if (isPromise(handlerResult)) {
yargs.getUsageInstance().cacheHelpMessage()
handlerResult
.then(value => {
if (yargs.handlerFinishCommand) {
return yargs.handlerFinishCommand(value)
if (handlerFinishCommand) {
handlerFinishCommand(value)
}
})
.catch(error => {
Expand All @@ -256,8 +257,8 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
}
})
} else {
if (yargs.handlerFinishCommand) {
yargs.handlerFinishCommand(handlerResult)
if (handlerFinishCommand) {
handlerFinishCommand(handlerResult)
}
}
}
Expand Down
34 changes: 19 additions & 15 deletions test/yargs.js
Expand Up @@ -2359,32 +2359,36 @@ describe('yargs dsl tests', () => {
describe('onFinishCommand', () => {
it('use with promise', (done) => {
const result = 'noop-result'
yargs(['noop']).command('noop', 'a noop command', noop, () => {
return Promise.resolve(result)
let calledTimes = 0
yargs(['noop']).command('noop', 'a noop command', noop, async () => {
return result
}).onFinishCommand(async (commandResult) => {
commandResult.should.eql(result)
calledTimes++
})
.parse('noop', (err, argv) => {
if (err) {
return done(err)
}
return done()
})
.parse('noop')

setTimeout(() => {
calledTimes.should.eql(1)
done()
}, 5)
})

it('use without promise', (done) => {
const result = 'noop-result'
let calledTimes = 0
yargs(['noop']).command('noop', 'a noop command', noop, () => {
return result
}).onFinishCommand(async (commandResult) => {
}).onFinishCommand((commandResult) => {
commandResult.should.eql(result)
calledTimes++
})
.parse('noop', (err, argv) => {
if (err) {
return done(err)
}
return done()
})
.parse('noop')

setTimeout(() => {
calledTimes.should.eql(1)
done()
}, 5)
})
})
})

0 comments on commit f1d41ef

Please sign in to comment.