From e0ee3def233b2ea316a0af83733f57b44440cf1b Mon Sep 17 00:00:00 2001 From: Dai Date: Mon, 29 Jun 2015 23:15:14 +0100 Subject: [PATCH] Added handling for undefined and null (fails on negate flag too) --- lib/chai/core/assertions.js | 12 +++++++----- test/expect.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index d38aaae..30e68a2 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -510,14 +510,16 @@ module.exports = function (chai, _) { 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; + if (obj == null) { + expected = flag(this, 'negate'); + } else if (Array.isArray(obj) || 'string' === typeof obj) { + expected = obj.length === 0; + } else if ('object' === typeof obj) { + expected = Object.keys(obj).length === 0; } this.assert( - !expected + expected , 'expected #{this} to be empty' , 'expected #{this} not to be empty' ); diff --git a/test/expect.js b/test/expect.js index 42b50e6..e152cf6 100644 --- a/test/expect.js +++ b/test/expect.js @@ -471,6 +471,35 @@ describe('expect', function () { err(function(){ expect({foo: 'bar'}).to.be.empty; }, "expected { foo: \'bar\' } to be empty"); + + err(function(){ + expect(0).to.be.empty; + }, "expected 0 to be empty"); + + err(function(){ + expect(null).to.be.empty; + }, "expected null to be empty"); + + err(function(){ + expect(undefined).to.be.empty; + }, "expected undefined to be empty"); + + err(function(){ + expect().to.be.empty; + }, "expected undefined to be empty"); + + err(function(){ + expect(null).to.not.be.empty; + }, "expected null not to be empty"); + + err(function(){ + expect(undefined).to.not.be.empty; + }, "expected undefined not to be empty"); + + err(function(){ + expect().to.not.be.empty; + }, "expected undefined not to be empty"); + }); it('NaN', function() {