Skip to content

Commit

Permalink
Remove undesirable methods before returning fake
Browse files Browse the repository at this point in the history
  • Loading branch information
mroderick committed Nov 11, 2017
1 parent 122a275 commit dc8e063
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions lib/sinon/fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,71 @@ function getError(value) {
return value instanceof Error ? value : new Error(value);
}

function cleanProxy(f) {
var undesirableProperties = [
"instantiateFake",
"callArg",
"callArgOn",
"callArgOnWith",
"callArgWith",
"invokeCallback",
"throwArg",
"withArgs",
"yield",
"yieldOn",
"yieldTo",
"yieldToOn"
];

undesirableProperties.forEach(function (key) {
delete f[key];
});

return f;
}

function wrapFunc(f) {
return cleanProxy(spy(f));
}

function fake(f) {
if (typeof func !== "undefined" && typeof func !== "function") {
throw new TypeError("Expected func argument to be a Function");
}

return spy(f);
return wrapFunc(f);
}

fake.returns = function returns(value) {
function f() {
return value;
}

return spy(f);
return wrapFunc(f);
};

fake.throws = function throws(value) {
function f() {
throw getError(value);
}

return spy(f);
return wrapFunc(f);
};

fake.resolves = function resolves(value) {
function f() {
return Promise.resolve(value);
}

return spy(f);
return wrapFunc(f);
};

fake.rejects = function rejects(value) {
function f() {
return Promise.reject(getError(value));
}

return spy(f);
return wrapFunc(f);
};

module.exports = fake;

0 comments on commit dc8e063

Please sign in to comment.