How to "Test" a Meteor.defer method?


#1

Hey guys,
Im just start testing my meteor project. But I’m facing a problem when to test a Meteor.defer method used for email sending.
Just want to test this piece of code on server side:

      Meteor.defer(function () {
        Accounts.sendVerificationEmail(userId)
      })

Cause i would like to spy like this

const sendEmailSpy = sinon.spy(Accounts, 'sendVerificationEmail') 

Any help will be highly appreciated. Thx in advance.


#2

The way I test any async function is this, within a “describe” and “it”:

it("modifies something I can check for", function (done) {
    const interval = setInterval(Meteor.bindEnvironment(() => {
          const result = spy.calledCount;

          if (result) {
            clearInterval(interval);
            expect(result).to.be.something;
            done();
          }
    }, 1000);
});

If it times out, you know something is wrong basically.
Within “describe” you can set a custom timeout using this.timeout(200000);


#3

Wouldn’t it be simpler just to stub Accounts.sendVerificationEmail and execute done() right after it ?
Especially that you don’t really want to call that method inside your test! Since it may send out an email.

If you want to mock Accounts, you should think about using dependency injection in your service: