From 8d53f4d202f86a11cbd3496469a1c5afd10cb836 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Sat, 2 Nov 2019 14:22:16 -0700 Subject: [PATCH] Fixed objectContaining to not match when the expected is the empty object and the actual is a non-object --- lib/jasmine-core/jasmine.js | 2 +- .../core/asymmetric_equality/ObjectContainingSpec.js | 12 ++++++++++-- src/core/asymmetric_equality/ObjectContaining.js | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 5321f2d0..25adbf80 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -2508,7 +2508,7 @@ getJasmineRequireObj().ObjectContaining = function(j$) { ObjectContaining.prototype.asymmetricMatch = function(other, customTesters) { if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); } - // TODO: This succeeds when sample is an empty object and other is a non-object, but should probably fail + if (typeof(other) !== 'object') { return false; } for (var property in this.sample) { if (!hasProperty(other, property) || diff --git a/spec/core/asymmetric_equality/ObjectContainingSpec.js b/spec/core/asymmetric_equality/ObjectContainingSpec.js index b17fd925..cdcbf19f 100644 --- a/spec/core/asymmetric_equality/ObjectContainingSpec.js +++ b/spec/core/asymmetric_equality/ObjectContainingSpec.js @@ -1,9 +1,17 @@ describe("ObjectContaining", function() { - it("matches any actual to an empty object", function() { + it("matches any object actual to an empty object", function() { var containing = new jasmineUnderTest.ObjectContaining({}); - expect(containing.asymmetricMatch("foo")).toBe(true); + expect(containing.asymmetricMatch({foo: 1})).toBe(true); + }); + + it("does not match when the actual is not an object", function() { + var containing = new jasmineUnderTest.ObjectContaining({}); + + [1, true, undefined, "a string"].forEach(function(actual) { + expect(containing.asymmetricMatch(actual)).toBe(false); + }); }); it("does not match an empty object actual", function() { diff --git a/src/core/asymmetric_equality/ObjectContaining.js b/src/core/asymmetric_equality/ObjectContaining.js index d2da5c05..a0d69135 100644 --- a/src/core/asymmetric_equality/ObjectContaining.js +++ b/src/core/asymmetric_equality/ObjectContaining.js @@ -30,7 +30,7 @@ getJasmineRequireObj().ObjectContaining = function(j$) { ObjectContaining.prototype.asymmetricMatch = function(other, customTesters) { if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); } - // TODO: This succeeds when sample is an empty object and other is a non-object, but should probably fail + if (typeof(other) !== 'object') { return false; } for (var property in this.sample) { if (!hasProperty(other, property) ||