From 9f6e9a7589eb26cac0b9b9de143bfb76a6c12281 Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Tue, 11 Sep 2018 17:47:13 +0100 Subject: [PATCH 1/2] Adds guard for empty properties in deepEqual when a matcher is provided 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..438f127bc 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 stub = sinon.stub(); + var dateOne = new Date("2018-07-01"); + var dateTwo = new Date("2018-07-31"); + + stub(dateOne); + + var calledWith = stub.calledWith(dateTwo); + assert.same(calledWith, false); + }); + }); }); From c68302769fdf6e7f836f919e59d933a607e190ca Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Thu, 13 Sep 2018 16:47:30 +0100 Subject: [PATCH 2/2] Updates issue test to use spy rather than stub --- test/issues/issues-test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/issues/issues-test.js b/test/issues/issues-test.js index 438f127bc..e801aa918 100644 --- a/test/issues/issues-test.js +++ b/test/issues/issues-test.js @@ -523,13 +523,13 @@ describe("issues", function () { describe("#1900 - calledWith returns false positive", function () { it("should return false when call args don't match", function () { - var stub = sinon.stub(); + var spy = sinon.spy(); var dateOne = new Date("2018-07-01"); var dateTwo = new Date("2018-07-31"); - stub(dateOne); + spy(dateOne); - var calledWith = stub.calledWith(dateTwo); + var calledWith = spy.calledWith(dateTwo); assert.same(calledWith, false); }); });