Reputation: 103
I am trying to see the output of sinon.spy()
but when I do a console.log( res.render )
I just see [Function]
in the console when I run the test.
const should = require( 'chai' ).should()
const sinon = require( 'sinon' )
const getIndex = ( req, res ) => {
res.render( 'index' )
}
describe( 'getIndex', function () {
it( 'should render index', function () {
let req = {}
let res = {
render: sinon.spy()
}
getIndex( req, res )
console.log( res.render ) // Expecting all properties from sinon.spy()
res.render.firstCall.args[ 0 ]
.should
.equal( 'index' )
} )
} )
EDIT:
I was expecting to see all of the properties that are available from sinon.spy()
. For instance, the firstCall.args[ 0 ]
property but I was wanting to see all of the properties. I know that they are listed at https://sinonjs.org/releases/v7.4.2/spies/ but I was hoping to see all of the properties at once since I am trying to get a better grasp on how sinon works for testing.
Upvotes: 2
Views: 1584
Reputation: 7634
Spies are functions because they're supposed to be used as function proxies or to replace functions on objects to track calls to them. The spy will internally track the calls to allow programmatic inspection.
You might not know that in Javascript functions are objects too.
In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions
Functions can have properties and the properties can be defined or configured like on "normal" objects.
The reason that the spy does not show attributes in the inspector, either by debugger or by a console.log()
, is, that the dev tools know its a function and functions are shown as such.
Dev tools obviously assume Function
objects (usually) don't have special properties and thus won't enumerate them.
You can observe that with basic functions too. Enter this into the console:
var f = function() { }
f.foo = 42;
f; // The property won't be printed
Though, you're able to inspect the spy object in the debugger when stopping at a breakpoint. You would add the reference to the "Watch" panel in the "Source" tab. Then you can open the function object and inspect the properties.
With the previous experiment done you could indeed inspect the function by adding a watch expression for f
.
A Sinon spy object under inspection:
Upvotes: 1
Reputation: 531
function
part (if you are in chrome dev console at least) it will show you the contents of the function code, not its results.res.render
is not a string or number or whatever, it is a function.res.render
.res.render
and pass it its necessary arguments, we will get the output of res.render()
.console.log(res.render("index"))
instead of just console.log(res.render)
Upvotes: 1