From c0ddf681a2cea1c78172467d493b67d07af4b65b Mon Sep 17 00:00:00 2001 From: KozakLordOfMatrix Date: Tue, 13 Oct 2026 10:32:27 +0300 Subject: [PATCH] Add to reset spy/stub state --- src/function.js | 7 ++++++- test/safe/function.test.js | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/function.js b/src/function.js index 7329f3ee..2c8f63b4 100644 --- a/src/function.js +++ b/src/function.js @@ -5,9 +5,14 @@ import stubbings from './store/stubbings' import imitate from './imitate' export default function func (nameOrFunc, __optionalName) { - return _.isFunction(nameOrFunc) + const testDouble = _.isFunction(nameOrFunc) ? imitate(nameOrFunc) : createTestDoubleNamed(nameOrFunc || __optionalName) + testDouble.reset = () => { + store.for(testDouble, false)?.stubbings.length && (store.for(testDouble, false).stubbings = []) + store.for(testDouble, false)?.calls.length && (store.for(testDouble, false).calls = []) + } + return testDouble } const createTestDoubleNamed = function (name) { diff --git a/test/safe/function.test.js b/test/safe/function.test.js index 8f6a3e88..5a80ed82 100644 --- a/test/safe/function.test.js +++ b/test/safe/function.test.js @@ -44,6 +44,17 @@ module.exports = { assert._isEqual(td.explain(result.foo).isTestDouble, true) assert._isEqual(result.bar, 42) + }, + 'resetting spy/stub state with .reset()' () { + const f = td.func() + td.when(f()).thenReturn(42) + assert._isEqual(f(), 42) + assert._isEqual(td.explain(f).callCount, 1) + + f.reset() + + assert._isEqual(f(), undefined) + assert._isEqual(td.explain(f).callCount, 0) } }