Skip to content

Commit

Permalink
fix returns does not override call through (#2567)
Browse files Browse the repository at this point in the history
* fix returns does not override call through
  • Loading branch information
rluvaton authored Nov 1, 2023
1 parent 08d0696 commit 5fde5ae
Show file tree
Hide file tree
Showing 2 changed files with 342 additions and 57 deletions.
128 changes: 71 additions & 57 deletions lib/sinon/default-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,127 +30,150 @@ function throwsException(fake, error, message) {
}
}

const SKIP_OPTIONS_FOR_YIELDS = {
skipReturn: true,
skipThrows: true,
};

function clear(fake, options) {
fake.fakeFn = undefined;

fake.callsThrough = undefined;
fake.callsThroughWithNew = undefined;

if (!options || !options.skipThrows) {
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.throwArgAt = undefined;
}

fake.callArgAt = undefined;
fake.callbackArguments = undefined;
fake.callbackContext = undefined;
fake.callArgProp = undefined;
fake.callbackAsync = undefined;

if (!options || !options.skipReturn) {
fake.returnValue = undefined;
fake.returnValueDefined = undefined;
fake.returnArgAt = undefined;
fake.returnThis = undefined;
}

fake.resolve = undefined;
fake.resolveThis = undefined;
fake.resolveArgAt = undefined;

fake.reject = undefined;
}

const defaultBehaviors = {
callsFake: function callsFake(fake, fn) {
clear(fake);

fake.fakeFn = fn;
fake.exception = undefined;
fake.exceptionCreator = undefined;
},

callsArg: function callsArg(fake, index) {
if (typeof index !== "number") {
throw new TypeError("argument index is not number");
}
clear(fake);

fake.callArgAt = index;
fake.callbackArguments = [];
fake.callbackContext = undefined;
fake.callArgProp = undefined;
fake.callbackAsync = false;
},

callsArgOn: function callsArgOn(fake, index, context) {
if (typeof index !== "number") {
throw new TypeError("argument index is not number");
}
clear(fake);

fake.callArgAt = index;
fake.callbackArguments = [];
fake.callbackContext = context;
fake.callArgProp = undefined;
fake.callbackAsync = false;
},

callsArgWith: function callsArgWith(fake, index) {
if (typeof index !== "number") {
throw new TypeError("argument index is not number");
}
clear(fake);

fake.callArgAt = index;
fake.callbackArguments = slice(arguments, 2);
fake.callbackContext = undefined;
fake.callArgProp = undefined;
fake.callbackAsync = false;
},

callsArgOnWith: function callsArgWith(fake, index, context) {
if (typeof index !== "number") {
throw new TypeError("argument index is not number");
}
clear(fake);

fake.callArgAt = index;
fake.callbackArguments = slice(arguments, 3);
fake.callbackContext = context;
fake.callArgProp = undefined;
fake.callbackAsync = false;
},

usingPromise: function usingPromise(fake, promiseLibrary) {
fake.promiseLibrary = promiseLibrary;
},

yields: function (fake) {
clear(fake, SKIP_OPTIONS_FOR_YIELDS);

fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice(arguments, 1);
fake.callbackContext = undefined;
fake.callArgProp = undefined;
fake.callbackAsync = false;
fake.fakeFn = undefined;
},

yieldsRight: function (fake) {
clear(fake, SKIP_OPTIONS_FOR_YIELDS);

fake.callArgAt = useRightMostCallback;
fake.callbackArguments = slice(arguments, 1);
fake.callbackContext = undefined;
fake.callArgProp = undefined;
fake.callbackAsync = false;
fake.fakeFn = undefined;
},

yieldsOn: function (fake, context) {
clear(fake, SKIP_OPTIONS_FOR_YIELDS);

fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice(arguments, 2);
fake.callbackContext = context;
fake.callArgProp = undefined;
fake.callbackAsync = false;
fake.fakeFn = undefined;
},

yieldsTo: function (fake, prop) {
clear(fake, SKIP_OPTIONS_FOR_YIELDS);

fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice(arguments, 2);
fake.callbackContext = undefined;
fake.callArgProp = prop;
fake.callbackAsync = false;
fake.fakeFn = undefined;
},

yieldsToOn: function (fake, prop, context) {
clear(fake, SKIP_OPTIONS_FOR_YIELDS);

fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice(arguments, 3);
fake.callbackContext = context;
fake.callArgProp = prop;
fake.callbackAsync = false;
fake.fakeFn = undefined;
},

throws: throwsException,
throwsException: throwsException,

returns: function returns(fake, value) {
clear(fake);

fake.returnValue = value;
fake.resolve = false;
fake.reject = false;
fake.returnValueDefined = true;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;
},

returnsArg: function returnsArg(fake, index) {
if (typeof index !== "number") {
throw new TypeError("argument index is not number");
}
clear(fake);

fake.returnArgAt = index;
},
Expand All @@ -159,38 +182,33 @@ const defaultBehaviors = {
if (typeof index !== "number") {
throw new TypeError("argument index is not number");
}
clear(fake);

fake.throwArgAt = index;
},

returnsThis: function returnsThis(fake) {
clear(fake);

fake.returnThis = true;
},

resolves: function resolves(fake, value) {
clear(fake);

fake.returnValue = value;
fake.resolve = true;
fake.resolveThis = false;
fake.reject = false;
fake.returnValueDefined = true;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;
},

resolvesArg: function resolvesArg(fake, index) {
if (typeof index !== "number") {
throw new TypeError("argument index is not number");
}
clear(fake);

fake.resolveArgAt = index;
fake.returnValue = undefined;
fake.resolve = true;
fake.resolveThis = false;
fake.reject = false;
fake.returnValueDefined = false;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;
},

rejects: function rejects(fake, error, message) {
Expand All @@ -203,34 +221,30 @@ const defaultBehaviors = {
} else {
reason = error;
}
clear(fake);

fake.returnValue = reason;
fake.resolve = false;
fake.resolveThis = false;
fake.reject = true;
fake.returnValueDefined = true;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;

return fake;
},

resolvesThis: function resolvesThis(fake) {
fake.returnValue = undefined;
fake.resolve = false;
clear(fake);

fake.resolveThis = true;
fake.reject = false;
fake.returnValueDefined = false;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;
},

callThrough: function callThrough(fake) {
clear(fake);

fake.callsThrough = true;
},

callThroughWithNew: function callThroughWithNew(fake) {
clear(fake);

fake.callsThroughWithNew = true;
},

Expand Down
Loading

0 comments on commit 5fde5ae

Please sign in to comment.