docusign-esign-node-client icon indicating copy to clipboard operation
docusign-esign-node-client copied to clipboard

Cannot stub non-existent property

Open rob-clother-saible opened this issue 6 months ago • 2 comments

I am trying to stub the EnvlopesApi prototype in my unit tests, but it doesn't seem possible to do this. However, ApiClient can be stubbed successfully.

  it('should not create an envelope when the user is changed from invited to invited', async () => {
    sinon.stub(ApiClient.prototype, 'requestJWTUserToken').resolves({
      body: { access_token: 'access-token' }
    });
    const createEnvelopeStub = sinon.stub(EnvelopesApi.prototype, 'createEnvelope').resolves({
      envelopeId: 'envelopeId'
    })
    const before = await createNewPendingUser(false);
    const after = await PendingUser.fromId(before.id);
    await callOnUpdate(triggers.onInvitationAccepted, before, after);
    assert(createEnvelopeStub.notCalled);
  });

The first stub does not generate an error; however, the second stub fails with a type error:

Cannot stub non-existent property createEnvelope

Why can I not stub the EnvelopesApi prototype, but the ApiClient prototype is stubbable?

rob-clother-saible avatar Jun 17 '25 09:06 rob-clother-saible

Hello Rob-clother-saible

Thank you for using Docusign. 

Try something like this:

// Stub the JWT token request
sinon.stub(ApiClient.prototype, 'requestJWTUserToken').resolves({
  body: { access_token: 'access-token'}
});   

// Create a fake instance with a stubbed method
envelopesApiInstance = {
  createEnvelope: sinon.stub().resolves({ envelopeId: 'envelopeId' })
};

// Stub the EnvelopesApi constructor to return our fake instance
sinon.replace(EnvelopesApi, 'constructor', sinon.fake()); // optional, just avoids constructor side effects
sinon.replace(require('docusign-esign'), 'EnvelopesApi', function () {
  return envelopesApiInstance;
   });
});

Best regards,

Adrian | Docusign Developer Support

Adrian-DS-Support avatar Jun 19 '25 04:06 Adrian-DS-Support

Thanks Adrian. I tried this suggestion, and several variants, and none of them worked. It looks like the @types/docusign-esign package doesn't reflect the Javascript types correctly.

rob-clother-saible avatar Jun 19 '25 08:06 rob-clother-saible