Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return returned value of invokant when using yields* and callsArg* #1724

Merged
merged 6 commits into from Mar 15, 2018
Merged
11 changes: 9 additions & 2 deletions lib/sinon/behavior.js
Expand Up @@ -92,9 +92,11 @@ function callCallback(behavior, args) {
func.apply(behavior.callbackContext, behavior.callbackArguments);
});
} else {
func.apply(behavior.callbackContext, behavior.callbackArguments);
return func.apply(behavior.callbackContext, behavior.callbackArguments);
}
}

return undefined;
}

var proto = {
Expand Down Expand Up @@ -125,7 +127,7 @@ var proto = {
},

invoke: function invoke(context, args) {
callCallback(this, args);
var returnValue = callCallback(this, args);

if (this.exception) {
throw this.exception;
Expand Down Expand Up @@ -156,7 +158,12 @@ var proto = {
return (this.promiseLibrary || Promise).reject(this.returnValue);
} else if (this.callsThrough) {
return this.stub.wrappedMethod.apply(context, args);
} else if (this.returnValueDefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is returnValueDefined being set? I don't see it anywhere else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its set in default-behaviour.js for returns, and resolves. Not sure it was the perfect answer. Original thought of typeof this.returnValue !== "undefined". Open to suggestions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have gone back and used typeof this.returnValue !== "undefined" as methods such as returnsThis don't set returnValueDefined

return this.returnValue;
} else if (this.yields) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a new property really required? I think checking for callsArgAt would also do, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would seem that callArgAt will work for this.

return returnValue;
}

return this.returnValue;
},

Expand Down
9 changes: 9 additions & 0 deletions lib/sinon/default-behaviors.js
Expand Up @@ -40,6 +40,7 @@ module.exports = {
throw new TypeError("argument index is not number");
}

fake.yields = true;
fake.callArgAt = index;
fake.callbackArguments = [];
fake.callbackContext = undefined;
Expand All @@ -52,6 +53,7 @@ module.exports = {
throw new TypeError("argument index is not number");
}

fake.yields = true;
fake.callArgAt = index;
fake.callbackArguments = [];
fake.callbackContext = context;
Expand All @@ -64,6 +66,7 @@ module.exports = {
throw new TypeError("argument index is not number");
}

fake.yields = true;
fake.callArgAt = index;
fake.callbackArguments = slice.call(arguments, 2);
fake.callbackContext = undefined;
Expand All @@ -76,6 +79,7 @@ module.exports = {
throw new TypeError("argument index is not number");
}

fake.yields = true;
fake.callArgAt = index;
fake.callbackArguments = slice.call(arguments, 3);
fake.callbackContext = context;
Expand All @@ -88,6 +92,7 @@ module.exports = {
},

yields: function (fake) {
fake.yields = true;
fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice.call(arguments, 1);
fake.callbackContext = undefined;
Expand All @@ -96,6 +101,7 @@ module.exports = {
},

yieldsRight: function (fake) {
fake.yields = true;
fake.callArgAt = useRightMostCallback;
fake.callbackArguments = slice.call(arguments, 1);
fake.callbackContext = undefined;
Expand All @@ -104,6 +110,7 @@ module.exports = {
},

yieldsOn: function (fake, context) {
fake.yields = true;
fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice.call(arguments, 2);
fake.callbackContext = context;
Expand All @@ -112,6 +119,7 @@ module.exports = {
},

yieldsTo: function (fake, prop) {
fake.yields = true;
fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice.call(arguments, 2);
fake.callbackContext = undefined;
Expand All @@ -120,6 +128,7 @@ module.exports = {
},

yieldsToOn: function (fake, prop, context) {
fake.yields = true;
fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice.call(arguments, 3);
fake.callbackContext = context;
Expand Down