diff --git a/lib/Unexpected.js b/lib/Unexpected.js index c4870fe47..fa5a8b5d5 100644 --- a/lib/Unexpected.js +++ b/lib/Unexpected.js @@ -174,6 +174,10 @@ var customTypePrototype = { Unexpected.prototype.addType = function (type) { var baseType; + if (typeof type.name !== 'string' || trim(type.name) === '') { + throw new Error('A custom type must be given a non-empty name'); + } + if (type.base) { baseType = utils.findFirst(this.types, function (t) { return t.name === type.base; diff --git a/test/unexpected.spec.js b/test/unexpected.spec.js index 184767fa8..41330e1e8 100644 --- a/test/unexpected.spec.js +++ b/test/unexpected.spec.js @@ -1754,6 +1754,7 @@ describe('unexpected', function () { beforeEach(function () { clonedExpect = expect.clone(); clonedExpect.addType({ + name: 'box', identify: function (obj) { return obj && typeof obj === 'object' && obj.isBox; }, @@ -1774,6 +1775,12 @@ describe('unexpected', function () { }); }); + it('throws an expection is the type has an empty or undefined name', function () { + expect(function () { + clonedExpect.addType({}); + }, 'to throw', 'A custom type must be given a non-empty name'); + }); + it('should use the equal defined by the type', function () { clonedExpect(box(123), 'to equal', box(123)); clonedExpect(box(123), 'not to equal', box(321));