Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change yields semantics to match stub behaviour
Fixes #1697
  • Loading branch information
mantoni authored and mroderick committed Apr 30, 2018
1 parent cf99c5a commit 7e2f05a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
26 changes: 9 additions & 17 deletions lib/sinon/fake.js
Expand Up @@ -3,6 +3,8 @@
var spy = require("./spy");
var nextTick = require("./util/core/next-tick");

var slice = Array.prototype.slice;

function getError(value) {
return value instanceof Error ? value : new Error(value);
}
Expand Down Expand Up @@ -74,8 +76,12 @@ fake.rejects = function rejects(value) {
return wrapFunc(f);
};

function yieldInternal(async, callback, values) {
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);
Expand All @@ -89,25 +95,11 @@ function yieldInternal(async, callback, values) {
}

fake.yields = function yields() {
var callback = Array.prototype.slice.call(arguments, 0, 1)[0];
var values = Array.prototype.slice.call(arguments, 1);

if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a Function");
}

return yieldInternal(false, callback, values);
return yieldInternal(false, slice.call(arguments));
};

fake.yieldsAsync = function yieldsAsync() {
var callback = Array.prototype.slice.call(arguments, 0, 1)[0];
var values = Array.prototype.slice.call(arguments, 1);

if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a Function");
}

return yieldInternal(true, callback, values);
return yieldInternal(true, slice.call(arguments));
};

module.exports = fake;
50 changes: 45 additions & 5 deletions test/fake-test.js
Expand Up @@ -160,25 +160,42 @@ describe("fake", function () {
describe(".yields", function () {
verifyProxy(fake.yields, noop, "42", "43");

it("should call the callback with the provided values", function () {
it("should call a callback with the provided values", function () {
var callback = sinon.spy();
var myFake = fake.yields(callback, "one", "two", "three");
var myFake = fake.yields("one", "two", "three");

myFake();
myFake(callback);

sinon.assert.calledOnce(callback);
sinon.assert.calledWith(callback, "one", "two", "three");
});

it("should call the last function argument", function () {
var callback = sinon.spy();
var myFake = fake.yields();

myFake(function () {}, callback);

sinon.assert.calledOnce(callback);
});

it("should throw if the last argument is not a function", function () {
var myFake = fake.yields();

assert.exception(function () {
myFake(function () {}, "not a function");
}, /TypeError: Expected last argument to be a function/);
});
});

describe(".yieldsAsync", function () {
verifyProxy(fake.yieldsAsync, noop, "42", "43");

it("should call the callback asynchronously with the provided values", function (done) {
var callback = sinon.spy();
var myFake = fake.yieldsAsync(callback, "one", "two", "three");
var myFake = fake.yieldsAsync("one", "two", "three");

myFake();
myFake(callback);

sinon.assert.notCalled(callback);

Expand All @@ -189,5 +206,28 @@ describe("fake", function () {
done();
}, 0);
});

it("should call the last function argument", function (done) {
var callback = sinon.spy();
var myFake = fake.yieldsAsync();

myFake(function () {}, callback);

sinon.assert.notCalled(callback);

setTimeout(function () {
sinon.assert.calledOnce(callback);

done();
}, 0);
});

it("should throw if the last argument is not a function", function () {
var myFake = fake.yieldsAsync();

assert.exception(function () {
myFake(function () {}, "not a function");
}, /TypeError: Expected last argument to be a function/);
});
});
});

0 comments on commit 7e2f05a

Please sign in to comment.