A Sinon sandbox (or sinon
instance) is passed from outside to script scope. Internal function (not a method) can be optionally spied/stubbed with Sinon sandbox.
Sinon is involved in some kind of monkey-patching here (not unit-testing) .Sinon sandbox concept fitted the use case really well - until this moment.
I proceed from the fact that function spy cannot be replaced with method spy. It is not a perfect scenario but the design cannot be changed.
const originalCallback = callback;
callback = sinonSandbox.spy(callback);
thirdPartyFn(callback);
// how can this be achieved?
// sinonSandbox.onRestore(() => thirdPartyFn(originalCallback));
How can the app be notified when sandbox is restored to restore spied function? Is there a hook to be launched on 'restore' event? Are there third-party Sinon extensions that may help?
As it appears, Sinon doesn't have a mechanism for notification on sandbox restore. Since the job of sandbox.restore()
is to call restore
method on each faked function, I've ended with patching fake's own restore
as the most suitable solution:
const originalCallback = callback;
callback = sinonSandbox.spy(callback);
const originalRestore = callback.restore;
callback.restore = function (...args) {
originalRestore.apply(this, args);
thirdPartyFn(originalCallback);
}