Skip to content

Commit

Permalink
Add enumerableProperty assertion.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 25, 2015
1 parent 1abc384 commit a75fdfa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
28 changes: 28 additions & 0 deletions lib/chai/core/assertions.js
Expand Up @@ -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)
*
Expand Down
12 changes: 11 additions & 1 deletion test/expect.js
Expand Up @@ -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' }
Expand Down Expand Up @@ -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');
Expand Down
10 changes: 10 additions & 0 deletions test/should.js
Expand Up @@ -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');
Expand Down

0 comments on commit a75fdfa

Please sign in to comment.