2

My function is sendMail i want to stub function mailjet and it has a method chain mailjet.post('send').request...

I want to assert callback is called on mail success or fail.

So how i stub this method chain?

var sendMail = function (templateName, callback) {

// From template name find template id of mailjet
mailingExternalTemplateModel.findMailingTemplateId(templateName, function (err, result) {

        const request = mailjet
            .post("send")
            .request(params)
        request
            .then((result) => {
                if (typeof callback === 'function') {
                    callback(null, result.body);
                }

            })
            .catch((err) => {
                if (typeof callback === 'function') {
                    callback(err, null);
                }
            })
    } else {
        callback(err, null);
    }
});};

I have done

    sinon.stub(mailjet, 'post').withArgs('send').returns(mailjetClient);

    sinon.stub(mailjetClient, 'request').returns(Promise);

But i got error TypeError: Attempted to wrap undefined property request as function

Apoorva Shah
  • 632
  • 1
  • 6
  • 15

1 Answers1

1

I'm the developer in charge of each Mailjet Wrappers, including the NodeJS one.

I'm actually updating each of them and adding features such as the possibility to make the call (or not). For the NodeJS version, a beta will be deployed on npm by tomorrow evening.

I'll update this answer with the modifications you'll have to make (which are few) once the beta is available.

If you are curious, you can still take a look at the modifications I made : https://github.com/mailjet/mailjet-apiv3-nodejs/pull/21

Aridjar
  • 301
  • 1
  • 5
  • 14