Skip to content

Commit c642b72

Browse files
committed
Support jQuery >= 3.0.0
1 parent c0d8876 commit c642b72

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Promise.resolve(2).should.eventually.be.within(Promise.resolve(1), Promise.resol
144144

145145
### Compatibility
146146

147-
Chai as Promised is compatible with all promises following the [Promises/A+ specification][spec]. Notably, jQuery's so-called “promises” are not up to spec, and Chai as Promised will not work with them. In particular, Chai as Promised makes extensive use of the standard [transformation behavior][] of `then`, which jQuery does not support.
147+
Chai as Promised is compatible with all promises following the [Promises/A+ specification][spec]. Notably, jQuery's promises were not up to spec before jQuery 3.0, and Chai as Promised will not work with them. In particular, Chai as Promised makes extensive use of the standard [transformation behavior][] of `then`, which jQuery<3.0 does not support.
148148

149149
### Working with Non-Promise–Friendly Test Runners
150150

lib/chai-as-promised.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
var Assertion = chai.Assertion;
3535
var assert = chai.assert;
3636

37-
function isJQueryPromise(thenable) {
38-
return typeof thenable.always === "function" &&
37+
function isLegacyJQueryPromise(thenable) {
38+
// jQuery promises are Promises/A+-compatible since 3.0.0. jQuery 3.0.0 is also the first version
39+
// to define the catch method.
40+
return typeof thenable.catch !== "function" &&
41+
typeof thenable.always === "function" &&
3942
typeof thenable.done === "function" &&
4043
typeof thenable.fail === "function" &&
4144
typeof thenable.pipe === "function" &&
@@ -47,9 +50,10 @@
4750
if (typeof assertion._obj.then !== "function") {
4851
throw new TypeError(utils.inspect(assertion._obj) + " is not a thenable.");
4952
}
50-
if (isJQueryPromise(assertion._obj)) {
51-
throw new TypeError("Chai as Promised is incompatible with jQuery's thenables, sorry! Please use a " +
52-
"Promises/A+ compatible library (see http://promisesaplus.com/).");
53+
if (isLegacyJQueryPromise(assertion._obj)) {
54+
throw new TypeError("Chai as Promised is incompatible with thenables of jQuery<3.0.0, sorry! Please " +
55+
"upgrade jQuery or use another Promises/A+ compatible library (see " +
56+
"http://promisesaplus.com/).");
5357
}
5458
}
5559

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"test": "npm run test-plugin && npm run test-intercompatibility",
2424
"test-plugin": "mocha",
2525
"test-intercompatibility": "mocha test-intercompatibility --opts test-intercompatibility/mocha.opts",
26+
"test-browser-jquery": "coffee ./test/browser/runner.coffee jquery",
2627
"test-browser-q": "coffee ./test/browser/runner.coffee q",
2728
"test-browser-when": "coffee ./test/browser/runner.coffee when",
2829
"lint": "jshint ./lib",

test/browser/libraries/jquery.coffee

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"use strict"
2+
3+
exports.name = "jQuery"
4+
5+
exports.uri = "https://code.jquery.com/jquery-3.0.0-beta1.js"
6+
7+
exports.adapter = """
8+
global.fulfilledPromise = function (value) {
9+
var deferred = jQuery.Deferred();
10+
deferred.resolve(value);
11+
return deferred.promise();
12+
};
13+
global.rejectedPromise = function (reason) {
14+
var deferred = jQuery.Deferred();
15+
deferred.reject(reason);
16+
return deferred.promise();
17+
};
18+
global.defer = function () {
19+
var deferred = jQuery.Deferred();
20+
return {
21+
promise: deferred.promise(),
22+
resolve: deferred.resolve,
23+
reject: deferred.reject,
24+
};
25+
};
26+
global.getPromise = function (deferred) {
27+
return deferred.promise();
28+
};
29+
global.waitAll = function (promises) {
30+
return jQuery.when
31+
.apply(null, promises)
32+
.then(function () {
33+
return Array.prototype.slice.call(arguments);
34+
});
35+
};
36+
"""

0 commit comments

Comments
 (0)