@small-tech/required
Version:
Add required argument checking to your functions.
28 lines (23 loc) • 799 B
JavaScript
const test = require('tape')
const required = require('..')
function needsPuppy (puppy = required('puppy')) {
// A puppy is required.
}
function needsGeneric (generic = required()) {
// We won’t know the name of what’s required.
}
test('required', t => {
t.plan(4)
try {
needsPuppy(/* oh no, no puppy */)
} catch (error) {
t.ok(error instanceof Error, 'error should be an Error instance')
t.strictEquals(error.message, 'Missing argument (puppy).', 'it should complain about the missing puppy')
t.strictEquals(error.stack.match(/at required/), null, 'error helper should not feature in stack trace')
}
try {
needsGeneric()
} catch (error) {
t.strictEquals(error.message, 'Missing argument.', 'it should complain about generic missing argument')
}
})