Skip to content

Commit

Permalink
Set fake.lastArg to last argument regardless of type
Browse files Browse the repository at this point in the history
"Falsy" last arguments to `fake` were ignored.

Rely on arguments length instead of value comparison.
  • Loading branch information
David Hunt authored and mroderick committed Mar 1, 2019
1 parent 8b8bddd commit e24daed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/sinon/fake.js
Expand Up @@ -37,7 +37,7 @@ function cleanProxy(f) {
var uuid = 0;
function wrapFunc(f) {
var fakeInstance = function() {
var lastArg = (arguments.length > 0 && arguments[arguments.length - 1]) || undefined;
var lastArg = arguments.length > 0 ? arguments[arguments.length - 1] : undefined;
var callback = lastArg && typeof lastArg === "function" ? lastArg : undefined;

/* eslint-disable no-use-before-define */
Expand Down
12 changes: 12 additions & 0 deletions test/fake-test.js
Expand Up @@ -143,6 +143,18 @@ describe("fake", function() {
f(46);
assert.equals(f.lastArg, 46);

f(false, true, 47, "string");
assert.equals(f.lastArg, "string");

f("string", false, true, 47);
assert.equals(f.lastArg, 47);

f(47, "string", false, true);
assert.equals(f.lastArg, true);

f(true, 47, "string", false);
assert.equals(f.lastArg, false);

f();
refute.defined(f.lastArg);
});
Expand Down
10 changes: 10 additions & 0 deletions test/issues/issues-test.js
Expand Up @@ -588,4 +588,14 @@ describe("issues", function() {
assert.equals(stub({}, [], "b"), "b");
});
});

describe("#1986", function() {
it("should not set `lastArg` to undefined when last argument is `false`", function() {
var fake = sinon.fake();

fake(99, false);

assert.equals(fake.lastArg, false);
});
});
});

0 comments on commit e24daed

Please sign in to comment.