3

If create an file with the following contents

const validateEmail = email => {
    sendEmail(email);
};

const sendEmail = email => {
  return true;
};

module.exports = {
  validateEmail,
  sendEmail,
};

And a test that tries to stub out the second function...

it('Should call sendEmail if a valid email is passed', () => {
  let sendEmailSpy = sinon.stub(checkEmail, 'sendEmail');
  checkEmail.validateEmail('acorrectemail@therightformat.com');
  assert.isTrue(sendEmailSpy.called);
});

It still calls the sendEmail function and the test fails

However, if I write the module.exports like this:

module.exports = {
  validateEmail(email) {
      this.sendEmail(email);
  },
  sendEmail(email) {
    return true;
  },
};

It stubs it correctly...Why?

grpcMe
  • 1,367
  • 5
  • 15
  • 28

1 Answers1

6

Short answer - context

Long answer - in the first scenario, the exported sendEmail function is not the same as the internal one that is used by validateEmail. The exported function becomes a new property of the object being exported and simply references the internal one.

In the second scenario, you explicitly reference the sendEmail function on the exported object (i.e. this.sendEmail(...)) from validateEmail therefore it will use the stubbed version.

Moral of the story - you can't stub something you can't see.

James
  • 80,725
  • 18
  • 167
  • 237
  • Riiight ok. Is there a way of stubbing using the first scenario? – grpcMe Dec 13 '17 at 15:18
  • @OmisNomis not based on your current implementation no, it needs to be externally accessible in order to stub it. What's wrong with the second approach? – James Dec 13 '17 at 15:23