Allowed async functions to be passed into spy#callFake
diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js
index e54501e..f30b7b0 100644
--- a/lib/jasmine-core/jasmine.js
+++ b/lib/jasmine-core/jasmine.js
@@ -5010,7 +5010,7 @@
* @param {Function} fn The function to invoke with the passed parameters.
*/
this.callFake = function(fn) {
- if(!j$.isFunction_(fn)) {
+ if(!(j$.isFunction_(fn) || j$.isAsyncFunction_(fn))) {
throw new Error('Argument passed to callFake should be a function, got ' + fn);
}
plan = fn;
diff --git a/spec/core/SpyStrategySpec.js b/spec/core/SpyStrategySpec.js
index 0965c04..2002af7 100644
--- a/spec/core/SpyStrategySpec.js
+++ b/spec/core/SpyStrategySpec.js
@@ -92,16 +92,40 @@
expect(returnValue).toEqual(67);
});
+ it("allows a fake async function to be called instead", async function(done) {
+ try {
+ var originalFn = jasmine.createSpy("original"),
+ fakeFn = jasmine.createSpy("fake").and.callFake(async function () { return 67; }),
+ spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
+ returnValue;
+
+ spyStrategy.callFake(fakeFn);
+ returnValue = await spyStrategy.exec();
+
+ expect(originalFn).not.toHaveBeenCalled();
+ expect(returnValue).toEqual(67);
+
+ done();
+ } catch (err) {
+ done.fail(err);
+ }
+ });
+
it('throws an error when a non-function is passed to callFake strategy', function() {
var originalFn = jasmine.createSpy('original'),
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
invalidFakes = [5, 'foo', {}, true, false, null, void 0, new Date(), /.*/];
spyOn(jasmineUnderTest, 'isFunction_').and.returnValue(false);
+ spyOn(jasmineUnderTest, 'isAsyncFunction_').and.returnValue(false);
expect(function () {
spyStrategy.callFake(function() {});
}).toThrowError(/^Argument passed to callFake should be a function, got/);
+
+ expect(function () {
+ spyStrategy.callFake(async function() {});
+ }).toThrowError(/^Argument passed to callFake should be a function, got/);
});
it("allows a return to plan stubbing after another strategy", function() {
diff --git a/src/core/SpyStrategy.js b/src/core/SpyStrategy.js
index 5af7cbe..b0c716f 100644
--- a/src/core/SpyStrategy.js
+++ b/src/core/SpyStrategy.js
@@ -88,7 +88,7 @@
* @param {Function} fn The function to invoke with the passed parameters.
*/
this.callFake = function(fn) {
- if(!j$.isFunction_(fn)) {
+ if(!(j$.isFunction_(fn) || j$.isAsyncFunction_(fn))) {
throw new Error('Argument passed to callFake should be a function, got ' + fn);
}
plan = fn;