Cannot stub non-existent property
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?
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
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.