TypeError: ‘undefined’ is not a function (evaluating ‘promise.then.bind(promise)’)
Hi everybody,
I had the same TypeError: 'undefined' is not a function (evaluating 'promise.then.bind(promise)')
error when testing promises with chai-as-promised on PhantomJS.
The issue is also described here, but i found it worth to share the solution.
Karma-chai-plugins team proposed a temporary function.bind.polyfill.js which works perfectly, enabling the bind method on PhantomJS as desired:
if (!Function.prototype.bind) {
/* eslint-disable no-extend-native */
Function.prototype.bind = function bind(that) { // .length is 1
var self = this;
if (typeof self !== 'function') {
throw new TypeError('Function.prototype.bind called on incompatible ' + self);
}
var args = Array.prototype.slice.call(arguments, 1); // for normal call
var bound = function () {
if (this instanceof bound) {
var result = self.apply(
this,
args.concat(Array.prototype.slice.call(arguments))
);
if (Object(result) === result) {
return result;
}
return this;
} else {
return self.apply(
that,
args.concat(Array.prototype.slice.call(arguments))
);
}
};
if (self.prototype) {
var Empty = function () {
};
Empty.prototype = self.prototype;
bound.prototype = new Empty();
}
return bound;
};
}
Include the polyfill in your karma.conf.js
and you will have the bind method on PhantomJS.
No comments yet.