Skip to content

Commit 285f06d

Browse files
committed
Merge branch 'promise-matching-1314' of https://github.com/sderickson/jasmine
2 parents 3aece2a + 6ddf645 commit 285f06d

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

lib/jasmine-core/jasmine.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
217217
return obj.nodeType > 0;
218218
};
219219

220+
j$.isPromise = function(obj) {
221+
return typeof jasmineGlobal.Promise !== 'undefined' && obj.constructor === jasmineGlobal.Promise;
222+
};
223+
220224
j$.fnNameFor = function(func) {
221225
if (func.name) {
222226
return func.name;
@@ -2683,6 +2687,12 @@ getJasmineRequireObj().matchersUtil = function(j$) {
26832687
diffBuilder.record(a, b);
26842688
return false;
26852689
}
2690+
2691+
var aIsPromise = j$.isPromise(a);
2692+
var bIsPromise = j$.isPromise(b);
2693+
if (aIsPromise && bIsPromise) {
2694+
return a === b;
2695+
}
26862696

26872697
// Assume equality for cyclic structures. The algorithm for detecting cyclic
26882698
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.

spec/core/matchers/matchersUtilSpec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ describe("matchersUtil", function() {
166166

167167
expect(jasmineUnderTest.matchersUtil.equals(a,b)).toBe(true);
168168
});
169+
170+
it("passes for equivalent Promises (GitHub issue #1314)", function() {
171+
if (typeof Promise === 'undefined') { return; }
172+
173+
var p1 = new Promise(function () {}),
174+
p2 = new Promise(function () {});
175+
176+
expect(jasmineUnderTest.matchersUtil.equals(p1, p1)).toBe(true);
177+
expect(jasmineUnderTest.matchersUtil.equals(p1, p2)).toBe(false);
178+
});
169179

170180
describe("when running in a browser", function() {
171181
function isNotRunningInBrowser() {

src/core/base.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
8787
return obj.nodeType > 0;
8888
};
8989

90+
j$.isPromise = function(obj) {
91+
return typeof jasmineGlobal.Promise !== 'undefined' && obj.constructor === jasmineGlobal.Promise;
92+
};
93+
9094
j$.fnNameFor = function(func) {
9195
if (func.name) {
9296
return func.name;

src/core/matchers/matchersUtil.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ getJasmineRequireObj().matchersUtil = function(j$) {
213213
diffBuilder.record(a, b);
214214
return false;
215215
}
216+
217+
var aIsPromise = j$.isPromise(a);
218+
var bIsPromise = j$.isPromise(b);
219+
if (aIsPromise && bIsPromise) {
220+
return a === b;
221+
}
216222

217223
// Assume equality for cyclic structures. The algorithm for detecting cyclic
218224
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.

0 commit comments

Comments
 (0)