-
Notifications
You must be signed in to change notification settings - Fork 992
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: console.warn() rather than throwing errors when api signatures a…
…re incorrect (#804)
- Loading branch information
Showing
3 changed files
with
151 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,143 @@ | ||
/* global describe, it */ | ||
|
||
const argsert = require('../lib/argsert') | ||
const expect = require('chai').expect | ||
const checkOutput = require('./helpers/utils').checkOutput | ||
|
||
require('chai').should() | ||
|
||
describe('Argsert', function () { | ||
it('does not throw exception if optional argument is not provided', function () { | ||
argsert('[object]', [].slice.call(arguments)) | ||
it('does not warn if optional argument is not provided', function () { | ||
var o = checkOutput(function () { | ||
argsert('[object]', [].slice.call(arguments)) | ||
}) | ||
|
||
o.warnings.length.should.equal(0) | ||
}) | ||
|
||
it('throws exception if wrong type is provided for optional argument', function () { | ||
function foo (opts) { | ||
argsert('[object|number]', [].slice.call(arguments)) | ||
} | ||
expect(function () { | ||
it('warn if wrong type is provided for optional argument', function () { | ||
var o = checkOutput(function () { | ||
function foo (opts) { | ||
argsert('[object|number]', [].slice.call(arguments)) | ||
} | ||
|
||
foo('hello') | ||
}).to.throw(/Invalid first argument. Expected object or number but received string./) | ||
}) | ||
|
||
o.warnings[0].should.match(/Invalid first argument. Expected object or number but received string./) | ||
}) | ||
|
||
it('does not throw exception if optional argument is valid', function () { | ||
function foo (opts) { | ||
argsert('[object]', [].slice.call(arguments)) | ||
} | ||
foo({foo: 'bar'}) | ||
it('does not warn if optional argument is valid', function () { | ||
var o = checkOutput(function () { | ||
function foo (opts) { | ||
argsert('[object]', [].slice.call(arguments)) | ||
} | ||
|
||
foo({foo: 'bar'}) | ||
}) | ||
|
||
o.warnings.length.should.equal(0) | ||
}) | ||
|
||
it('throws exception if required argument is not provided', function () { | ||
expect(function () { | ||
it('warns if required argument is not provided', function () { | ||
var o = checkOutput(function () { | ||
argsert('<object>', [].slice.call(arguments)) | ||
}).to.throw(/Not enough arguments provided. Expected 1 but received 0./) | ||
}) | ||
|
||
o.warnings[0].should.match(/Not enough arguments provided. Expected 1 but received 0./) | ||
}) | ||
|
||
it('throws exception if required argument is of wrong type', function () { | ||
function foo (opts) { | ||
argsert('<object>', [].slice.call(arguments)) | ||
} | ||
expect(function () { | ||
it('warns if required argument is of wrong type', function () { | ||
var o = checkOutput(function () { | ||
function foo (opts) { | ||
argsert('<object>', [].slice.call(arguments)) | ||
} | ||
|
||
foo('bar') | ||
}).to.throw(/Invalid first argument. Expected object but received string./) | ||
}) | ||
|
||
o.warnings[0].should.match(/Invalid first argument. Expected object but received string./) | ||
}) | ||
|
||
it('supports a combination of required and optional arguments', function () { | ||
function foo (opts) { | ||
argsert('<array> <string|object> [string|object]', [].slice.call(arguments)) | ||
} | ||
foo([], 'foo', {}) | ||
var o = checkOutput(function () { | ||
function foo (opts) { | ||
argsert('<array> <string|object> [string|object]', [].slice.call(arguments)) | ||
} | ||
|
||
foo([], 'foo', {}) | ||
}) | ||
|
||
o.warnings.length.should.equal(0) | ||
}) | ||
|
||
it('throws an exception if too many arguments are provided', function () { | ||
function foo (expected) { | ||
argsert('<array> [batman]', [].slice.call(arguments)) | ||
} | ||
expect(function () { | ||
it('warns if too many arguments are provided', function () { | ||
var o = checkOutput(function () { | ||
function foo (expected) { | ||
argsert('<array> [batman]', [].slice.call(arguments)) | ||
} | ||
|
||
foo([], 33, 99) | ||
}).to.throw(/Too many arguments provided. Expected max 2 but received 3./) | ||
}) | ||
|
||
o.warnings[0].should.match(/Too many arguments provided. Expected max 2 but received 3./) | ||
}) | ||
|
||
it('configures function to accept 0 parameters, if only arguments object is provided', function () { | ||
function foo (expected) { | ||
argsert([].slice.call(arguments)) | ||
} | ||
expect(function () { | ||
var o = checkOutput(function () { | ||
function foo (expected) { | ||
argsert([].slice.call(arguments)) | ||
} | ||
|
||
foo(99) | ||
}).to.throw(/Too many arguments provided. Expected max 0 but received 1./) | ||
}) | ||
|
||
o.warnings[0].should.match(/Too many arguments provided. Expected max 0 but received 1./) | ||
}) | ||
|
||
it('allows for any type if * is provided', function () { | ||
function foo (opts) { | ||
argsert('<*>', [].slice.call(arguments)) | ||
} | ||
foo('bar') | ||
var o = checkOutput(function () { | ||
function foo (opts) { | ||
argsert('<*>', [].slice.call(arguments)) | ||
} | ||
|
||
foo('bar') | ||
}) | ||
|
||
o.warnings.length.should.equal(0) | ||
}) | ||
|
||
it('should ignore trailing undefined values', function () { | ||
function foo (opts) { | ||
argsert('<*>', [].slice.call(arguments)) | ||
} | ||
foo('bar', undefined, undefined) | ||
var o = checkOutput(function () { | ||
function foo (opts) { | ||
argsert('<*>', [].slice.call(arguments)) | ||
} | ||
|
||
foo('bar', undefined, undefined) | ||
}) | ||
|
||
o.warnings.length.should.equal(0) | ||
}) | ||
|
||
it('should not ignore undefined values that are not trailing', function () { | ||
function foo (opts) { | ||
argsert('<*>', [].slice.call(arguments)) | ||
} | ||
expect(function () { | ||
var o = checkOutput(function () { | ||
function foo (opts) { | ||
argsert('<*>', [].slice.call(arguments)) | ||
} | ||
|
||
foo('bar', undefined, undefined, 33) | ||
}).to.throw(/Too many arguments provided. Expected max 1 but received 4./) | ||
}) | ||
|
||
o.warnings[0].should.match(/Too many arguments provided. Expected max 1 but received 4./) | ||
}) | ||
|
||
it('supports null as special type', function () { | ||
function foo (arg) { | ||
argsert('<null>', [].slice.call(arguments)) | ||
} | ||
foo(null) | ||
var o = checkOutput(function () { | ||
function foo (arg) { | ||
argsert('<null>', [].slice.call(arguments)) | ||
} | ||
foo(null) | ||
}) | ||
|
||
o.warnings.length.should.equal(0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters