Skip to content

Commit

Permalink
fix: accept single function for middleware
Browse files Browse the repository at this point in the history
* Fixed type check in .middleware() method

`if (typeof callback === 'object')` should be `if (typeof callback === 'function')`

See Issue #1214 for details

* middewareFactory accepts a function, not an object

According to [the docs for the `.middleware()` method](http://yargs.js.org/docs/#api-middlewarecallbacks):

> The `callbacks` parameter can be a function or a list of functions

See Issue #1214 for more details

* Added/Updated middleware tests

I added a new test to verify that the `.middleware()` method works when a single function is passed, rather than an array of functions.

Also enhanced an existing test to ensure that _all_ middleware is run before commands, regardless of whether it was added as an array of functions or a single function.
  • Loading branch information
JamesMessinger authored and aorinevo committed Sep 16, 2018
1 parent 6c6c27d commit 66fd6f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/middleware.js
Expand Up @@ -2,7 +2,7 @@ module.exports = function (globalMiddleware, context) {
return function (callback) {
if (Array.isArray(callback)) {
Array.prototype.push.apply(globalMiddleware, callback)
} else if (typeof callback === 'object') {
} else if (typeof callback === 'function') {
globalMiddleware.push(callback)
}
return context
Expand Down
39 changes: 32 additions & 7 deletions test/middleware.js
Expand Up @@ -24,11 +24,30 @@ describe('middleware', () => {
it('should add a single callback to global middleware', () => {
const globalMiddleware = []

middlewareFactory(globalMiddleware)({})
middlewareFactory(globalMiddleware)(function () {})

globalMiddleware.should.have.lengthOf(1)
})

it('runs the middleware before reaching the handler', function (done) {
yargs(['mw'])
.middleware(function (argv) {
argv.mw = 'mw'
})
.command(
'mw',
'adds func to middleware',
function () {},
function (argv) {
// we should get the argv filled with data from the middleware
argv.mw.should.equal('mw')
return done()
}
)
.exitProcess(false) // defaults to true.
.parse()
})

it('runs all middleware before reaching the handler', function (done) {
yargs(['mw'])
.middleware([
Expand Down Expand Up @@ -56,11 +75,9 @@ describe('middleware', () => {

it('should be able to register middleware regardless of when middleware is called', function (done) {
yargs(['mw'])
.middleware([
function (argv) {
argv.mw1 = 'mw1'
}
])
.middleware(function (argv) {
argv.mw1 = 'mw1'
})
.command(
'mw',
'adds func list to middleware',
Expand All @@ -69,12 +86,20 @@ describe('middleware', () => {
// we should get the argv filled with data from the middleware
argv.mw1.should.equal('mw1')
argv.mw2.should.equal('mw2')
argv.mw3.should.equal('mw3')
argv.mw4.should.equal('mw4')
return done()
}
)
.middleware(function (argv) {
argv.mw2 = 'mw2'
})
.middleware([
function (argv) {
argv.mw2 = 'mw2'
argv.mw3 = 'mw3'
},
function (argv) {
argv.mw4 = 'mw4'
}
])
.exitProcess(false) // defaults to true.
Expand Down

0 comments on commit 66fd6f7

Please sign in to comment.