Fixed objectContaining to not match when the expected is the empty object and the actual is a non-object

This commit is contained in:
Steve Gravrock
2019-11-02 14:22:16 -07:00
parent 71631a6529
commit 8d53f4d202
3 changed files with 12 additions and 4 deletions

View File

@@ -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) ||

View File

@@ -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() {

View File

@@ -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) ||