Skip to content

Commit

Permalink
Add .lastArg and .callback to spy call
Browse files Browse the repository at this point in the history
  • Loading branch information
mroderick committed Mar 30, 2018
1 parent f5e03eb commit dc6b605
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/release-source/release/spy-call.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ The call's `this` value.
Array of received arguments.


### `spyCall.callback`

This property is a convenience for a call's callback.

When the last argument in a call is a `Function`, then `callback` will reference that. Otherwise it will be `undefined`.

```js
var spy = sinon.spy();
var callback = function () {};

spy(1, 2, 3, callback);

spy.lastCall.callback === callback;
// true
```

#### `spyCall.lastArg`

This property is a convenience for the last argument of the call.

```js
var spy = sinon.spy();
var date = new Date();

spy(1, 2, date);

spy.lastCall.lastArg === date;
// true
```

### `spyCall.exception`

Exception thrown, if any.
Expand Down
6 changes: 6 additions & 0 deletions lib/sinon/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,16 @@ function createSpyCall(spy, thisValue, args, returnValue, exception, id, errorWi
if (typeof id !== "number") {
throw new TypeError("Call id is not a number");
}

var proxyCall = Object.create(callProto);
var lastArg = args.length > 0 && args[args.length - 1] || undefined;
var callback = lastArg && typeof lastArg === "function" ? lastArg : undefined;

proxyCall.proxy = spy;
proxyCall.thisValue = thisValue;
proxyCall.args = args;
proxyCall.lastArg = lastArg;
proxyCall.callback = callback;
proxyCall.returnValue = returnValue;
proxyCall.exception = exception;
proxyCall.callId = id;
Expand Down
35 changes: 35 additions & 0 deletions test/call-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,41 @@ describe("sinonSpy.call", function () {
});
});

describe(".callback", function () {
it("it should be a reference for the callback", function () {
var spy = sinonSpy();
var callback1 = function () {};
var callback2 = function () {};

spy(1, 2, 3, callback1);
assert.equals(spy.getCall(0).callback, callback1);

spy(1, 2, 3, callback2);
assert.equals(spy.getCall(1).callback, callback2);

spy(1, 2, 3);
assert.equals(spy.getCall(2).callback, undefined);
});
});

describe(".lastArg", function () {
it("should be the last argument from the call", function () {
var spy = sinonSpy();

spy(41, 42, 43);
assert.equals(spy.getCall(0).lastArg, 43);

spy(44, 45);
assert.equals(spy.getCall(1).lastArg, 45);

spy(46);
assert.equals(spy.getCall(2).lastArg, 46);

spy();
assert.equals(spy.getCall(3).lastArg, undefined);
});
});

describe("call.yieldTest", function () {
beforeEach(spyCallCallSetup);

Expand Down

0 comments on commit dc6b605

Please sign in to comment.