Testing packages that rely on Meteor settings


#1

I’m trying to test my package that needs a value from Meteor.settings. But when I start the test, I get this error:

Exception while invoking method 'sendData' TypeError: Cannot read property 'appKey' of undefined

My test:

 it('sends event data in proper form', function () {
    Meteor.settings = {
      myApp: {
        appKey: 'thisisafakeappkey',
      },
    };

    const post = sinon.stub(HTTP, 'post');

    trackEvent(event, data, user);
    HTTP.post.restore();
}

The method:

Meteor.methods({
    'sendData'({ payload }) {
      const appKey = Meteor.settings.myapp.appKey;
      if (!appKey) throw new Meteor.Error('No app key found in Meteor.settings.');

      const withAppKey = Object.assign({ appKey }, payload);

      HTTP.post(getRemoteUrl(), {
        data: withAppKey,
      }, (err, res) => {
        if (err) console.log('err', err);
        else console.log('res', res);
      });
    },
  });

#2

can you use sinon to stub the settings as well?


#3

I thought Sinon only stubbed methods? Could you provide an example? And I thought a simple reassignment of a global variable would work with Meteor.settings


#4

Hmm looking at your test I think you’re doing an integration test (i.e. client to server) am I guessing right? I guess you’d need to do something with chimp setup (I am not sure I haven’t gotten chimp to work correctly myself). I would recommend you think of it a different way if you’re unit testing, how about you just call the method directly?


#5

Can you show us how your test and the sendData method relate to each other? Your code doesn’t show an association (does trackEvent call sendData?).