From 7746b52cfe64de7c614de3d004c398ba448cef1d Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Sun, 10 Oct 2021 03:24:41 +0000 Subject: [PATCH] spy/stub: Add support for lastCall Also added missing license file --- LICENSE | 13 +++++++++++++ lib/sinon.mjs | 3 +++ package.json | 1 + test/sinon.test.mjs | 9 +++++++++ 4 files changed, 26 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5c93f45 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/lib/sinon.mjs b/lib/sinon.mjs index f975ad2..c938ef2 100644 --- a/lib/sinon.mjs +++ b/lib/sinon.mjs @@ -17,6 +17,7 @@ export function stub(returnFunc = null) { let func = function(...args) { func.called = true calls.push(args) + func.lastCall = args if (func.callCount < indexMap.length) { func[indexMap[func.callCount]] = args } @@ -25,12 +26,14 @@ export function stub(returnFunc = null) { return returner(...args) } } + func.lastCall = null func.called = false func.callCount = 0 func.onCall = function(i) { return calls[i] } func.reset = function() { + func.lastCall = null func.called = false func.callCount = 0 for (let i = 0; i < indexMap.length; i++) { diff --git a/package.json b/package.json index 0274ea0..01ef552 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "index.mjs", "cli.mjs", "README.md", + "LICENSE", "lib" ] } diff --git a/test/sinon.test.mjs b/test/sinon.test.mjs index 28b9056..b1e6ee4 100644 --- a/test/sinon.test.mjs +++ b/test/sinon.test.mjs @@ -11,6 +11,7 @@ import { spy, stub } from '../index.mjs' assert.strictEqual(spyer.firstCall, null) assert.strictEqual(spyer.secondCall, null) assert.strictEqual(spyer.thirdCall, null) + assert.strictEqual(spyer.lastCall, null) spyer(1) spyer(2) @@ -23,6 +24,8 @@ import { spy, stub } from '../index.mjs' assert.notStrictEqual(spyer.firstCall, null) assert.notStrictEqual(spyer.secondCall, null) assert.notStrictEqual(spyer.thirdCall, null) + assert.notStrictEqual(spyer.lastCall, null) + assert.strictEqual(spyer.lastCall[0], 5) spyer.reset() @@ -31,6 +34,7 @@ import { spy, stub } from '../index.mjs' assert.strictEqual(spyer.firstCall, null) assert.strictEqual(spyer.secondCall, null) assert.strictEqual(spyer.thirdCall, null) + assert.strictEqual(spyer.lastCall, null) }) t.test('should keep track of call count', function() { @@ -74,8 +78,12 @@ import { spy, stub } from '../index.mjs' assert.notOk(spyer.called) spyer(assertFirstArgs) + assert.strictEqual(spyer.lastCall[0], assertFirstArgs) spyer(assertSecondArgs) + assert.strictEqual(spyer.lastCall[0], assertSecondArgs) spyer(assertThirdArgs[0], assertThirdArgs[1]) + assert.strictEqual(spyer.lastCall[0], assertThirdArgs[0]) + assert.strictEqual(spyer.lastCall[1], assertThirdArgs[1]) assert.strictEqual(spyer.callCount, 3) assert.strictEqual(spyer.onCall(0)[0], assertFirstArgs) @@ -226,6 +234,7 @@ t.describe('#stub()', function() { assert.strictEqual(s(3), 'test') assert.strictEqual(s(4), 'test') assert.strictEqual(s(5), 'test') + assert.strictEqual(s.lastCall[0], 5) assert.notStrictEqual(s.callCount, 0) assert.ok(s.called)