Skip to content

Commit

Permalink
fix: async middleware was called twice (#1422)
Browse files Browse the repository at this point in the history
  • Loading branch information
mleguen authored and bcoe committed Sep 18, 2019
1 parent 0be43d2 commit 9a42b63
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/middleware.js
Expand Up @@ -40,8 +40,7 @@ function applyMiddleware (argv, yargs, middlewares, beforeValidation) {
const beforeValidationError = new Error('middleware cannot return a promise when applyBeforeValidation is true')
return middlewares
.reduce((accumulation, middleware) => {
if (middleware.applyBeforeValidation !== beforeValidation &&
!isPromise(accumulation)) {
if (middleware.applyBeforeValidation !== beforeValidation) {
return accumulation
}

Expand Down
30 changes: 30 additions & 0 deletions test/middleware.js
Expand Up @@ -145,6 +145,36 @@ describe('middleware', () => {
.exitProcess(false)
.parse()
})

it('calls an async middleware only once for nested subcommands', (done) => {
let callCount = 0
let argv = yargs('cmd subcmd')
.command(
'cmd',
'cmd command',
function (yargs) {
yargs.command(
'subcmd',
'subcmd command',
function (yargs) {}
)
}
)
.middleware((argv) => new Promise((resolve) => {
callCount++
resolve(argv)
}))
.parse()

if (!(argv instanceof Promise)) done('argv should be a Promise')

argv
.then(() => {
callCount.should.equal(1)
done()
})
.catch(err => done(err))
})
})

// see: https://github.com/yargs/yargs/issues/1281
Expand Down

0 comments on commit 9a42b63

Please sign in to comment.