Skip to content

Commit

Permalink
Throw error when instanceof is passed something that is not a functio…
Browse files Browse the repository at this point in the history
…n as constructor
  • Loading branch information
lucasfcosta committed Jan 11, 2017
1 parent 877dde8 commit 01b1485
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,12 @@ module.exports = function (chai, _) {

function assertInstanceOf (constructor, msg) {
if (msg) flag(this, 'message', msg);
var constructorType = constructor === null ? 'null' : typeof constructor;

if (constructorType !== 'function') {
throw new Error('The instanceof assertion needs a constructor but ' + constructorType + ' was given.');
}

var name = _.getName(constructor);
this.assert(
flag(this, 'object') instanceof constructor
Expand Down
60 changes: 60 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,36 @@ describe('assert', function () {
function Foo(){}
assert.instanceOf(new Foo(), Foo);

err(function(){
assert.instanceOf(new Foo(), 1);
}, "The instanceof assertion needs a constructor but number was given.");

err(function(){
assert.instanceOf(new Foo(), 'batman');
}, "The instanceof assertion needs a constructor but string was given.");

err(function(){
assert.instanceOf(new Foo(), {});
}, "The instanceof assertion needs a constructor but object was given.");

err(function(){
assert.instanceOf(new Foo(), true);
}, "The instanceof assertion needs a constructor but boolean was given.");

err(function(){
assert.instanceOf(new Foo(), null);
}, "The instanceof assertion needs a constructor but null was given.");

err(function(){
assert.instanceOf(new Foo(), undefined);
}, "The instanceof assertion needs a constructor but undefined was given.");

if (typeof Symbol !== 'undefined') {
err(function(){
assert.instanceOf(new Foo(), Symbol());
}, "The instanceof assertion needs a constructor but symbol was given.");
}

err(function () {
assert.instanceOf(5, Foo);
}, "expected 5 to be an instance of Foo");
Expand All @@ -156,6 +186,36 @@ describe('assert', function () {
function Foo(){}
assert.notInstanceOf(new Foo(), String);

err(function(){
assert.notInstanceOf(new Foo(), 1);
}, "The instanceof assertion needs a constructor but number was given.");

err(function(){
assert.notInstanceOf(new Foo(), 'batman');
}, "The instanceof assertion needs a constructor but string was given.");

err(function(){
assert.notInstanceOf(new Foo(), {});
}, "The instanceof assertion needs a constructor but object was given.");

err(function(){
assert.notInstanceOf(new Foo(), true);
}, "The instanceof assertion needs a constructor but boolean was given.");

err(function(){
assert.notInstanceOf(new Foo(), null);
}, "The instanceof assertion needs a constructor but null was given.");

err(function(){
assert.notInstanceOf(new Foo(), undefined);
}, "The instanceof assertion needs a constructor but undefined was given.");

if (typeof Symbol !== 'undefined') {
err(function(){
assert.notInstanceOf(new Foo(), Symbol());
}, "The instanceof assertion needs a constructor but symbol was given.");
}

err(function () {
assert.notInstanceOf(new Foo(), Foo);
}, "expected {} to not be an instance of Foo");
Expand Down
30 changes: 30 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,36 @@ describe('expect', function () {
function Foo(){}
expect(new Foo()).to.be.an.instanceof(Foo);

err(function(){
expect(new Foo()).to.an.instanceof(1);
}, "The instanceof assertion needs a constructor but number was given.");

err(function(){
expect(new Foo()).to.an.instanceof('batman');
}, "The instanceof assertion needs a constructor but string was given.");

err(function(){
expect(new Foo()).to.an.instanceof({});
}, "The instanceof assertion needs a constructor but object was given.");

err(function(){
expect(new Foo()).to.an.instanceof(true);
}, "The instanceof assertion needs a constructor but boolean was given.");

err(function(){
expect(new Foo()).to.an.instanceof(null);
}, "The instanceof assertion needs a constructor but null was given.");

err(function(){
expect(new Foo()).to.an.instanceof(undefined);
}, "The instanceof assertion needs a constructor but undefined was given.");

if (typeof Symbol !== 'undefined') {
err(function(){
expect(new Foo()).to.an.instanceof(Symbol());
}, "The instanceof assertion needs a constructor but symbol was given.");
}

err(function(){
expect(3).to.an.instanceof(Foo, 'blah');
}, "blah: expected 3 to be an instance of Foo");
Expand Down
30 changes: 30 additions & 0 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,36 @@ describe('should', function() {
function Foo(){}
new Foo().should.be.an.instanceof(Foo);

err(function(){
new Foo().should.be.an.instanceof(1);
}, "The instanceof assertion needs a constructor but number was given.");

err(function(){
new Foo().should.be.an.instanceof('batman');
}, "The instanceof assertion needs a constructor but string was given.");

err(function(){
new Foo().should.be.an.instanceof({});
}, "The instanceof assertion needs a constructor but object was given.");

err(function(){
new Foo().should.be.an.instanceof(true);
}, "The instanceof assertion needs a constructor but boolean was given.");

err(function(){
new Foo().should.be.an.instanceof(null);
}, "The instanceof assertion needs a constructor but null was given.");

err(function(){
new Foo().should.be.an.instanceof(undefined);
}, "The instanceof assertion needs a constructor but undefined was given.");

if (typeof Symbol !== 'undefined') {
err(function(){
new Foo().should.be.an.instanceof(Symbol());
}, "The instanceof assertion needs a constructor but symbol was given.");
}

err(function(){
(3).should.an.instanceof(Foo, 'blah');
}, "blah: expected 3 to be an instance of Foo");
Expand Down

0 comments on commit 01b1485

Please sign in to comment.