Skip to content

Commit

Permalink
Fix requiredArgs error message
Browse files Browse the repository at this point in the history
  • Loading branch information
akgondber committed Mar 23, 2020
1 parent 14b706b commit 3ef7604
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/_lib/requiredArgs/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export default function requiredArgs(required, args) {
if (args.length < required) {
throw new TypeError(
required + ' argument' + required > 1
? 's'
: '' + ' required, but only ' + args.length + ' present'
required +
' argument' +
(required > 1 ? 's' : '') +
' required, but only ' +
args.length +
' present'
)
}
}
37 changes: 37 additions & 0 deletions src/_lib/requiredArgs/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import requiredArgs from '.'

describe('requiredArgs', function() {
function wrapperFn(required) {
// $ExpectedMistake
return function() {
requiredArgs(required, arguments)
}
}
const twoArgsRequired = wrapperFn(2)

context('with correct number of passed arguments', function() {
it('does not throw an error', function() {
assert.doesNotThrow(() => twoArgsRequired(1, 2))
})
})

context('with wrong number of arguments', function() {
it('throws correct error message', function() {
assert.throws(
function() {
twoArgsRequired(1)
},
function(err) {
return (
err instanceof TypeError &&
err.message === '2 arguments required, but only 1 present'
)
}
)
})
})
})

0 comments on commit 3ef7604

Please sign in to comment.