Skip to content

Commit

Permalink
test: add tests for alias behavior, based on conversations today (#1291)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Feb 12, 2019
1 parent f45a817 commit 95700d6
Showing 1 changed file with 76 additions and 3 deletions.
79 changes: 76 additions & 3 deletions test/middleware.js
Expand Up @@ -208,8 +208,34 @@ describe('middleware', () => {
.parse()
})

describe('applyBeforeValidation', () => {
it('runs before validation when "true"', function (done) {
it('applies aliases before middleware is called', (done) => {
yargs(['mw', '--foo', '99'])
.middleware(function (argv) {
argv.f.should.equal(99)
argv.mw = 'mw'
})
.command(
'mw',
'adds func to middleware',
function (yargs) {
yargs.middleware((argv) => {
argv.f.should.equal(99)
argv.mw2 = 'mw2'
})
},
function (argv) {
argv.mw.should.equal('mw')
argv.mw2.should.equal('mw2')
return done()
}
)
.alias('foo', 'f')
.exitProcess(false)
.parse()
})

describe('applyBeforeValidation=true', () => {
it('runs before validation', function (done) {
yargs(['mw'])
.middleware(function (argv) {
argv.mw = 'mw'
Expand All @@ -232,7 +258,7 @@ describe('middleware', () => {
.parse()
})

it('throws an error if "true" and async function provided', function () {
it('throws an error if async function provided', function () {
expect(() => {
yargs(['mw'])
.middleware([async function (argv) {
Expand Down Expand Up @@ -278,5 +304,52 @@ describe('middleware', () => {
.exitProcess(false)
.parse()
})

it('applies aliases before middleware is called, for global middleware', (done) => {
yargs(['mw', '--foo', '99'])
.middleware(function (argv) {
argv.f.should.equal(99)
argv.mw = 'mw'
}, true)
.command(
'mw',
'adds func to middleware',
{
'mw': {
'demand': true
}
},
function (argv) {
argv.mw.should.equal('mw')
return done()
}
)
.alias('foo', 'f')
.exitProcess(false)
.parse()
})

it('applies aliases before middleware is called, when middleware is added in builder', (done) => {
yargs(['mw', '--foo', '99'])
.command(
'mw',
'adds func to middleware',
function (yargs) {
yargs
.middleware((argv) => {
argv.f.should.equal(99)
argv.mw = 'mw'
}, true)
.demand('mw')
},
function (argv) {
argv.mw.should.equal('mw')
return done()
}
)
.alias('foo', 'f')
.exitProcess(false)
.parse()
})
})
})

0 comments on commit 95700d6

Please sign in to comment.