Skip to content

Commit

Permalink
Merge pull request #475 from Daveloper87/empty_handle_undefined
Browse files Browse the repository at this point in the history
Added handling for undefined and null on expect empty (fails on negate flag too)
  • Loading branch information
keithamus committed Jun 30, 2015
2 parents 084a419 + 97cdd32 commit 95e89ee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
13 changes: 3 additions & 10 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,10 @@ module.exports = function (chai, _) {
*/

Assertion.addProperty('empty', function () {
var obj = flag(this, 'object')
, expected = obj;

if (Array.isArray(obj) || 'string' === typeof object) {
expected = obj.length;
} else if (typeof obj === 'object') {
expected = Object.keys(obj).length;
}

var obj = flag(this, 'object');
new Assertion(obj).to.exist;
this.assert(
!expected
Object.keys(Object(obj)).length === 0
, 'expected #{this} to be empty'
, 'expected #{this} not to be empty'
);
Expand Down
25 changes: 25 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,31 @@ describe('expect', function () {
err(function(){
expect({foo: 'bar'}).to.be.empty;
}, "expected { foo: \'bar\' } to be empty");

err(function(){
expect(null).to.be.empty;
}, "expected null to exist");

err(function(){
expect(undefined).to.be.empty;
}, "expected undefined to exist");

err(function(){
expect().to.be.empty;
}, "expected undefined to exist");

err(function(){
expect(null).to.not.be.empty;
}, "expected null to exist");

err(function(){
expect(undefined).to.not.be.empty;
}, "expected undefined to exist");

err(function(){
expect().to.not.be.empty;
}, "expected undefined to exist");

});

it('property(name)', function(){
Expand Down

0 comments on commit 95e89ee

Please sign in to comment.