From a75fdfa0b4e5185733d43868e92ee104003bc7b9 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 24 Mar 2015 21:29:56 -0700 Subject: [PATCH] Add `enumerableProperty` assertion. --- lib/chai/core/assertions.js | 28 ++++++++++++++++++++++++++++ test/expect.js | 12 +++++++++++- test/should.js | 10 ++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index ac838837e..8b9fb1811 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -888,6 +888,34 @@ module.exports = function (chai, _) { Assertion.addMethod('ownProperty', assertOwnProperty); Assertion.addMethod('haveOwnProperty', assertOwnProperty); + /** + * ### .enumerableProperty(name) + * + * Asserts that the target has an enumerable property `name`. + * + * expect('test').not.to.have.enumerableProperty('length'); + * + * @name enumerableProperty + * @alias haveEnumerableProperty + * @param {String} name + * @param {String} message _optional_ + * @api public + */ + + function assertEnumerableProperty (name, msg) { + if (msg) flag(this, 'message', msg); + var obj = flag(this, 'object'); + var descriptor = Object.getOwnPropertyDescriptor(Object(obj), name); + this.assert( + descriptor && descriptor.enumerable + , 'expected #{this} to have enumerable property ' + _.inspect(name) + , 'expected #{this} not to have enumerable property ' + _.inspect(name) + ); + } + + Assertion.addMethod('enumerableProperty', assertEnumerableProperty); + Assertion.addMethod('haveEnumerableProperty', assertEnumerableProperty); + /** * ### .length(value) * diff --git a/test/expect.js b/test/expect.js index 0d11c67e6..38cf95270 100644 --- a/test/expect.js +++ b/test/expect.js @@ -467,7 +467,7 @@ describe('expect', function () { err(function(){ expect(deepObj).to.have.deep.property('teas[3].tea', 'bar'); }, "expected { Object (green, teas) } to have a deep property 'teas[3].tea'"); - + var arr = [ [ 'chai', 'matcha', 'konacha' ] , [ { tea: 'chai' } @@ -531,6 +531,16 @@ describe('expect', function () { }, "blah: expected { length: 12 } to not have own property 'length'"); }); + it('enumerableProperty(name)', function(){ + expect('test').not.to.have.enumerableProperty('length'); + expect('test').not.to.haveEnumerableProperty('length'); + expect({ length: 12 }).to.have.enumerableProperty('length'); + + err(function(){ + expect({ length: 12 }).not.to.have.enumerableProperty('length', 'blah'); + }, "blah: expected { length: 12 } not to have enumerable property 'length'"); + }); + it('string()', function(){ expect('foobar').to.have.string('bar'); expect('foobar').to.have.string('foo'); diff --git a/test/should.js b/test/should.js index 4c18e03b0..33f95d63e 100644 --- a/test/should.js +++ b/test/should.js @@ -396,6 +396,16 @@ describe('should', function() { }, "blah: expected { length: 12 } to not have own property 'length'"); }); + it('enumerableProperty(name)', function(){ + 'test'.should.not.have.enumerableProperty('length'); + 'test'.should.not.haveEnumerableProperty('length'); + ({ length: 12 }).should.have.enumerableProperty('length'); + + err(function(){ + ({ length: 12 }).should.not.have.enumerableProperty('length', 'blah'); + }, "blah: expected { length: 12 } not to have enumerable property 'length'"); + }); + it('string()', function(){ 'foobar'.should.contain.string('bar'); 'foobar'.should.contain.string('foo');