I had the following stubs running perfectly before
sinon.stub(console, 'log', () => {
// Check what the arguments holds
// And either console.info it or do nothing
});
For example, adding console.info(arguments)
inside there, would show me whatever console.log
was getting.
With version 2xx
I switched to callsFake
:
sinon.stub(console, 'log').callsFake(() => {
// Check what the arguments holds
// And either console.info it or do nothing
});
This now longer works. console.info(arguments)
has bazaar values, and nothing to do with what console.log
is passing.
What am I doing wrong?!
The sinon. stub() substitutes the real function and returns a stub object that you can configure using methods like callsFake() . Stubs also have a callCount property that tells you how many times the stub was called.
A test spy is a function that records arguments, return value, the value of this and exception thrown (if any) for all its calls.
Sinon replaces the whole request module (or part of it) during the test execution, making the stub available via require('request') and then restore it after the tests are finished? require('request') will return the same (object) reference, that was created inside the "request" module, every time it's called.
The arrow function you're passing to callsFake
doesn't receive the arguments
object as you would normally expect in a regular function.
From MDN
An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target.
Either change your arrow function to a regular anonymous function (function() {...}
) or use the spread operator to explicitly unpack the arguments:
sinon.stub(console, 'log')
console.log.callsFake((...args) => {
console.info(args)
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With