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

Add usingPromise() method on fakes to fix issue #2293 #2301

Merged
merged 2 commits into from Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
109 changes: 60 additions & 49 deletions lib/sinon/fake.js
Expand Up @@ -37,70 +37,81 @@ function wrapFunc(f) {
return proxy;
}

function fake(f) {
if (arguments.length > 0 && typeof f !== "function") {
throw new TypeError("Expected f argument to be a Function");
}
function fakeClass() {
var promiseLib = Promise;

return wrapFunc(f);
}
function fake(f) {
if (arguments.length > 0 && typeof f !== "function") {
throw new TypeError("Expected f argument to be a Function");
}

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

return wrapFunc(f);
};

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

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

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

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

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

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

function yieldInternal(async, values) {
function f() {
var callback = arguments[arguments.length - 1];
if (typeof callback !== "function") {
throw new TypeError("Expected last argument to be a function");
fake.rejects = function rejects(value) {
function f() {
return promiseLib.reject(getError(value));
}
if (async) {
nextTick(function() {

return wrapFunc(f);
};

fake.usingPromise = function usingPromise(promiseLibrary) {
promiseLib = promiseLibrary;
return fake;
};

function yieldInternal(async, values) {
function f() {
var callback = arguments[arguments.length - 1];
if (typeof callback !== "function") {
throw new TypeError("Expected last argument to be a function");
}
if (async) {
nextTick(function() {
callback.apply(null, values);
});
} else {
callback.apply(null, values);
});
} else {
callback.apply(null, values);
}
}

return wrapFunc(f);
}

return wrapFunc(f);
}
fake.yields = function yields() {
return yieldInternal(false, slice(arguments));
};

fake.yields = function yields() {
return yieldInternal(false, slice(arguments));
};
fake.yieldsAsync = function yieldsAsync() {
return yieldInternal(true, slice(arguments));
};

fake.yieldsAsync = function yieldsAsync() {
return yieldInternal(true, slice(arguments));
};
return fake;
}

module.exports = fake;
module.exports = fakeClass();
43 changes: 43 additions & 0 deletions test/fake-test.js
Expand Up @@ -474,4 +474,47 @@ describe("fake", function() {
assert.same(myFake.printf, proxy.printf);
});
});

describe(".usingPromise", function() {
before(requirePromiseSupport);

it("should exist and be a function", function() {
assert(fake.usingPromise);
assert.isFunction(fake.usingPromise);
});

it("should set the promise used by resolve", function() {
var promise = {
resolve: function(value) {
return Promise.resolve(value);
}
};
var object = {};

var myFake = fake.usingPromise(promise).resolves(object);

return myFake().then(function(actual) {
assert.same(actual, object, "Same object resolved");
});
});

it("should set the promise used by reject", function() {
var promise = {
reject: function(err) {
return Promise.reject(err);
}
};
var reason = new Error();

var myFake = fake.usingPromise(promise).rejects(reason);

return myFake()
.then(function() {
referee.fail("this should not resolve");
})
.catch(function(actual) {
assert.same(actual, reason, "Same object resolved");
});
});
});
});