Allow generator functions to be passed to .and.callFake

Fixes #1848.
This commit is contained in:
Steve Gravrock
2020-08-29 13:05:57 -07:00
parent e0eb4755cb
commit 53d8073707
7 changed files with 57 additions and 2 deletions
+11 -1
View File
@@ -244,6 +244,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
return j$.isA_('AsyncFunction', value); return j$.isA_('AsyncFunction', value);
}; };
j$.isGeneratorFunction_ = function(value) {
return j$.isA_('GeneratorFunction', value);
};
j$.isTypedArray_ = function(value) { j$.isTypedArray_ = function(value) {
return ( return (
j$.isA_('Float32Array', value) || j$.isA_('Float32Array', value) ||
@@ -8308,7 +8312,13 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
* @param {Function} fn The function to invoke with the passed parameters. * @param {Function} fn The function to invoke with the passed parameters.
*/ */
SpyStrategy.prototype.callFake = function(fn) { SpyStrategy.prototype.callFake = function(fn) {
if (!(j$.isFunction_(fn) || j$.isAsyncFunction_(fn))) { if (
!(
j$.isFunction_(fn) ||
j$.isAsyncFunction_(fn) ||
j$.isGeneratorFunction_(fn)
)
) {
throw new Error( throw new Error(
'Argument passed to callFake should be a function, got ' + fn 'Argument passed to callFake should be a function, got ' + fn
); );
+11
View File
@@ -334,6 +334,17 @@ describe('SpyStrategy', function() {
}).toThrowError(/^Argument passed to callFake should be a function, got/); }).toThrowError(/^Argument passed to callFake should be a function, got/);
}); });
it('allows generator functions to be passed to callFake strategy', function() {
jasmine.getEnv().requireGeneratorFunctions();
var generator = jasmine.getEnv().makeGeneratorFunction('yield "ok";'),
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: function() {} });
spyStrategy.callFake(generator);
expect(spyStrategy.exec().next().value).toEqual('ok');
});
it('allows a return to plan stubbing after another strategy', function() { it('allows a return to plan stubbing after another strategy', function() {
var originalFn = jasmine.createSpy('original'), var originalFn = jasmine.createSpy('original'),
fakeFn = jasmine.createSpy('fake').and.returnValue(67), fakeFn = jasmine.createSpy('fake').and.returnValue(67),
+22
View File
@@ -0,0 +1,22 @@
(function(env) {
function getGeneratorFuncCtor() {
try {
eval('var func = function*() {}');
} catch (e) {
return null;
}
return Object.getPrototypeOf(func).constructor;
}
env.makeGeneratorFunction = function(text) {
var GeneratorFunction = getGeneratorFuncCtor();
return new GeneratorFunction(text || '');
};
env.requireGeneratorFunctions = function() {
if (!getGeneratorFuncCtor()) {
env.pending('Environment does not support generator functions');
}
};
})(jasmine.getEnv());
+1
View File
@@ -18,6 +18,7 @@ module.exports = {
specFiles: ['**/*[Ss]pec.js', '!npmPackage/**/*'], specFiles: ['**/*[Ss]pec.js', '!npmPackage/**/*'],
helpers: [ helpers: [
'helpers/asyncAwait.js', 'helpers/asyncAwait.js',
'helpers/generator.js',
'helpers/BrowserFlags.js', 'helpers/BrowserFlags.js',
'helpers/checkForMap.js', 'helpers/checkForMap.js',
'helpers/checkForSet.js', 'helpers/checkForSet.js',
+1
View File
@@ -6,6 +6,7 @@
], ],
"helpers": [ "helpers": [
"helpers/asyncAwait.js", "helpers/asyncAwait.js",
"helpers/generator.js",
"helpers/checkForMap.js", "helpers/checkForMap.js",
"helpers/checkForSet.js", "helpers/checkForSet.js",
"helpers/checkForSymbol.js", "helpers/checkForSymbol.js",
+7 -1
View File
@@ -168,7 +168,13 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
* @param {Function} fn The function to invoke with the passed parameters. * @param {Function} fn The function to invoke with the passed parameters.
*/ */
SpyStrategy.prototype.callFake = function(fn) { SpyStrategy.prototype.callFake = function(fn) {
if (!(j$.isFunction_(fn) || j$.isAsyncFunction_(fn))) { if (
!(
j$.isFunction_(fn) ||
j$.isAsyncFunction_(fn) ||
j$.isGeneratorFunction_(fn)
)
) {
throw new Error( throw new Error(
'Argument passed to callFake should be a function, got ' + fn 'Argument passed to callFake should be a function, got ' + fn
); );
+4
View File
@@ -76,6 +76,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
return j$.isA_('AsyncFunction', value); return j$.isA_('AsyncFunction', value);
}; };
j$.isGeneratorFunction_ = function(value) {
return j$.isA_('GeneratorFunction', value);
};
j$.isTypedArray_ = function(value) { j$.isTypedArray_ = function(value) {
return ( return (
j$.isA_('Float32Array', value) || j$.isA_('Float32Array', value) ||