From 6ddf64568eaa1176427056010edec7b00f21eac5 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Mon, 26 Jun 2017 19:10:19 -0700 Subject: [PATCH] Add Promise checking to eq Fixes #1314 --- spec/core/matchers/matchersUtilSpec.js | 10 ++++++++++ src/core/base.js | 4 ++++ src/core/matchers/matchersUtil.js | 6 ++++++ 3 files changed, 20 insertions(+) diff --git a/spec/core/matchers/matchersUtilSpec.js b/spec/core/matchers/matchersUtilSpec.js index f30621a0..08a50506 100644 --- a/spec/core/matchers/matchersUtilSpec.js +++ b/spec/core/matchers/matchersUtilSpec.js @@ -166,6 +166,16 @@ describe("matchersUtil", function() { expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true); }); + + it("passes for equivalent Promises (GitHub issue #1314)", function() { + if (typeof Promise === 'undefined') { return; } + + var p1 = new Promise(function () {}), + p2 = new Promise(function () {}); + + expect(jasmineUnderTest.matchersUtil.equals(p1, p1)).toBe(true); + expect(jasmineUnderTest.matchersUtil.equals(p1, p2)).toBe(false); + }); describe("when running in a browser", function() { function isNotRunningInBrowser() { diff --git a/src/core/base.js b/src/core/base.js index 949f9f29..3955c6a5 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -75,6 +75,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) { return obj.nodeType > 0; }; + j$.isPromise = function(obj) { + return (typeof Promise !== 'undefined') && obj.constructor === Promise; + }; + j$.fnNameFor = function(func) { if (func.name) { return func.name; diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index 1ed2ee34..98735f2c 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -209,6 +209,12 @@ getJasmineRequireObj().matchersUtil = function(j$) { diffBuilder.record(a, b); return false; } + + var aIsPromise = j$.isPromise(a); + var bIsPromise = j$.isPromise(b); + if (aIsPromise && bIsPromise) { + return a === b; + } // Assume equality for cyclic structures. The algorithm for detecting cyclic // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.