Skip to content

Commit

Permalink
Adds guard for empty properties in deepEqual when a matcher is provid…
Browse files Browse the repository at this point in the history
…ed (sinonjs#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 sinonjs#1900
  • Loading branch information
mrwillihog authored and Franck Romano committed Oct 1, 2019
1 parent 340240e commit 097fa67
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/sinon/util/core/deep-equal.js
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions test/issues/issues-test.js
Expand Up @@ -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);
});
});
});

0 comments on commit 097fa67

Please sign in to comment.