From 7ccf82bc7c01e0b1f326e4c62784199aa0a2959e Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Tue, 2 Aug 2016 00:04:59 +0300 Subject: [PATCH] Fix 'empty' assertion called on null --- lib/chai/core/assertions.js | 2 +- test/expect.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index f3bc6d6f4..8d7176ca8 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -437,7 +437,7 @@ module.exports = function (chai, _) { if (Array.isArray(obj) || 'string' === typeof object) { expected = obj.length; - } else if (typeof obj === 'object') { + } else if (obj && 'object' === typeof obj) { expected = Object.keys(obj).length; } diff --git a/test/expect.js b/test/expect.js index 05950761a..864cc450a 100644 --- a/test/expect.js +++ b/test/expect.js @@ -420,6 +420,7 @@ describe('expect', function () { function FakeArgs() {}; FakeArgs.prototype.length = 0; + expect(null).to.be.empty; expect('').to.be.empty; expect('foo').not.to.be.empty; expect([]).to.be.empty; @@ -429,6 +430,10 @@ describe('expect', function () { expect({}).to.be.empty; expect({foo: 'bar'}).not.to.be.empty; + err(function(){ + expect(null).not.to.be.empty; + }, "expected null not to be empty"); + err(function(){ expect('').not.to.be.empty; }, "expected \'\' not to be empty");