Wednesday, 23 October 2019

How can I get a property attached to a global to stub properly with sinon?

I have a helper.js that loads before my tests:

before(async function() {
    this.timeout(30000)
    global.db = require(`${process.cwd()}/models`)()
    ...

Then in my test, I have:

describe.only('Phone Library', () => {
    let updateCallSpy
    beforeEach(() => {
        sinon.stub(twilioClient.calls, 'create').resolves({})
        updateCallSpy = sinon.stub(global.db.models.Call, 'update').resolves(true)
            // sinon.stub(global.db.models.Conversation, 'update').resolves({})
    })

The twilioClient.calls.create stubs properly. But the global.db.models.Call.update does not.

In my actual code, I'm using it like:

        await global.db.models.Call.update({ status: newCallStatus }, { where: { id: foundConversation.Call.id } })

When I console.log(global.db.models.Call), it simply outputs Call. However, the .update function is there and does what it's supposed to do (a Sequelize model that updates).

I'm sure It's something terribly obvious, so any pointers would be greatly appreciated. Thanks.



from How can I get a property attached to a global to stub properly with sinon?

No comments:

Post a Comment