Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #1335: async middleware called twice #1422

Merged
merged 3 commits into from Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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