From 097fa67d841c92c400a9469d240c8c9d546a6b6d Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Thu, 13 Sep 2018 22:22:02 +0100 Subject: [PATCH] Adds guard for empty properties in deepEqual when a matcher is provided (#1901) In JavaScript every acts like the "for all" quantifier in mathematics. In particular, for an empty array, it returns true. (It is vacuously true that all elements of the empty set satisfy any given condition.) This is not the intended behaviour for deepEqual, we should treat empty properties as falsy. Resolves #1900 --- lib/sinon/util/core/deep-equal.js | 5 +++-- test/issues/issues-test.js | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/sinon/util/core/deep-equal.js b/lib/sinon/util/core/deep-equal.js index 5c9ac9762..d1d2b6270 100644 --- a/lib/sinon/util/core/deep-equal.js +++ b/lib/sinon/util/core/deep-equal.js @@ -38,11 +38,12 @@ var deepEqual = module.exports = function deepEqual(a, b, matcher) { } if (matcher) { - var allKeysMatch = every(Object.keys(a), function (key) { + var keys = Object.keys(a); + var allKeysMatch = every(keys, function (key) { return matcher(a[key], b[key]); }); - return allKeysMatch; + return keys.length > 0 && allKeysMatch; } return false; diff --git a/test/issues/issues-test.js b/test/issues/issues-test.js index cace92937..e801aa918 100644 --- a/test/issues/issues-test.js +++ b/test/issues/issues-test.js @@ -520,4 +520,17 @@ describe("issues", function () { stub.restore(); }); }); + + describe("#1900 - calledWith returns false positive", function () { + it("should return false when call args don't match", function () { + var spy = sinon.spy(); + var dateOne = new Date("2018-07-01"); + var dateTwo = new Date("2018-07-31"); + + spy(dateOne); + + var calledWith = spy.calledWith(dateTwo); + assert.same(calledWith, false); + }); + }); });