Sinon stores a reference to the original function in the wrappedMethod
property of the stub (docs were just recently added in 2020). This can be called in the fake method.
sinon.stub(Array.prototype, 'sort').callsFake(
// Don't use arrow function => syntax if you need to use 'this' below!
function () {
console.log(`sorting array ${this}`);
return Array.prototype.sort.wrappedMethod.apply(this, arguments);
}
);
const array = ['C', 'A', 'B'].sort();
console.log(`sorted array is ${array}`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/7.3.2/sinon.min.js"></script>
And so the OP's code would be:
sinon.stub(servicecore.ServiceWrapper.prototype, '_invoke').callsFake(function(method, name, body, headers, callback) {
console.log('---- ServiceWrapper._invoke called! ----');
return servicecore.ServiceWrapper.prototype._invoke.wrappedMethod.apply(this, arguments);
});