Using Sinon stubs, I have successfully stubbed functions along the project, using the traditional stubbing syntax:
const permissionsStub = sinon.stub(invitation, 'update')
sinon.assert.calledOnce(permissionsStub)
Now I am trying to Stub a built-in function of NodeJS (findIndex, indexOf, etc..), without success, as it should be passed with the in the first argument of Sinon's stub() function call, and as that is a NodeJS's core function, what needs to be passed?
I have tried creating an empty stub and then assigning it directly to the function in the code, but I think there is a simpler way to try and spy/stub/mock built-in NodeJS functions.
I get this error by trying to pass a few arguments that might target it correctly, as it doesn't succeed mounting:
TypeError: Cannot read property 'returns' of undefined
How can I target those functions in a simpler way using Chai and Sinon?
So eventually I got it to work using the following code (rewire
is being used to get to the inner unexported functions of the file):
const permissionIndex = invitation.__get__('permissionIndex')
permissionsStub.findIndex = () => {
return { id: 1 }
}
expect(permissionIndex(1, permissionsStub)).to.be.an('object')
expect(permissionIndex(1, permissionsStub)).to.have.deep.property('id', 1)