Skip to content

Commit

Permalink
fix: middleware should work regardless of when method is called
Browse files Browse the repository at this point in the history
fixes #1178
  • Loading branch information
aorinevo authored and bcoe committed Aug 11, 2018
1 parent 18b2fcb commit 664b265
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/command.js
Expand Up @@ -19,7 +19,8 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
let aliases = []
handler = handler || (() => {})
middlewares = middlewares || []
middlewares = globalMiddleware.concat(middlewares)
globalMiddleware.push(...middlewares)
middlewares = globalMiddleware
if (Array.isArray(cmd)) {
aliases = cmd.slice(1)
cmd = cmd[0]
Expand Down
27 changes: 27 additions & 0 deletions test/middleware.js
Expand Up @@ -53,4 +53,31 @@ describe('middleware', () => {
.exitProcess(false) // defaults to true.
.parse()
})

it('should be able to register middleware regardless of when middleware is called', function (done) {
yargs(['mw'])
.middleware([
function (argv) {
argv.mw1 = 'mw1'
}
])
.command(
'mw',
'adds func list to middleware',
function () {},
function (argv) {
// we should get the argv filled with data from the middleware
argv.mw1.should.equal('mw1')
argv.mw2.should.equal('mw2')
return done()
}
)
.middleware([
function (argv) {
argv.mw2 = 'mw2'
}
])
.exitProcess(false) // defaults to true.
.parse()
})
})

0 comments on commit 664b265

Please sign in to comment.