From 53b0752ff00733c87e3714769e5cece0f7de84f3 Mon Sep 17 00:00:00 2001 From: Julie Ralph Date: Tue, 3 Feb 2015 13:43:49 -0800 Subject: [PATCH] Allow arrays from different frames or contexts to be equal --- spec/core/matchers/matchersUtilSpec.js | 27 ++++++++++++++++++++++++++ src/core/matchers/matchersUtil.js | 13 ++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/spec/core/matchers/matchersUtilSpec.js b/spec/core/matchers/matchersUtilSpec.js index 7ff4ada91..26f8ff610 100644 --- a/spec/core/matchers/matchersUtilSpec.js +++ b/spec/core/matchers/matchersUtilSpec.js @@ -171,6 +171,33 @@ describe("matchersUtil", function() { expect(j$.matchersUtil.equals(a,b)).toBe(true); }); + it("passes for equivalent objects from different vm contexts", function() { + if (typeof require !== 'function') { + return; + } + var vm = require('vm'); + var sandbox = { + obj: null + }; + vm.runInNewContext('obj = {a: 1, b: 2}', sandbox); + + expect(j$.matchersUtil.equals(sandbox.obj, {a: 1, b: 2})).toBe(true); + }); + + it("passes for equivalent arrays from different vm contexts", function() { + if (typeof require !== 'function') { + return; + } + + var vm = require('vm'); + var sandbox = { + arr: null + }; + vm.runInNewContext('arr = [1, 2]', sandbox); + + expect(j$.matchersUtil.equals(sandbox.arr, [1, 2])).toBe(true); + }); + it("passes when Any is used", function() { var number = 3, anyNumber = new j$.Any(Number); diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index 49ec1145e..e1f3ab451 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -167,11 +167,14 @@ getJasmineRequireObj().matchersUtil = function(j$) { if (result) { // Objects with different constructors are not equivalent, but `Object`s - // from different frames are. - var aCtor = a.constructor, bCtor = b.constructor; - if (aCtor !== bCtor && !(isFunction(aCtor) && (aCtor instanceof aCtor) && - isFunction(bCtor) && (bCtor instanceof bCtor))) { - return false; + // or `Array`s from different frames are. + var areArrays = className === '[object Array]'; + if (!areArrays) { + var aCtor = a.constructor, bCtor = b.constructor; + if (aCtor !== bCtor && !(isFunction(aCtor) && aCtor instanceof aCtor && + isFunction(bCtor) && bCtor instanceof bCtor)) { + return false; + } } // Deep compare objects. for (var key in a) {