Search code examples
reactjsunit-testingmeteorjestjs

How would I mock out Meteor.settings with jest?


Meteor.settings is being used in a react class component that I am trying to mock. In the constructor of that component, Meteor.settings is accessed to get a url. I am using the moduleMapper from jest to mock Meteor.settings like so:

const Meteor = {
  settings: {
    public: {
      URL: "http://testing:0000",
    },
  },
};

export default Meteor;

In my test file I am importing my class component. After running jest, I am getting the following error: TypeError: Cannot read property 'settings' of undefined.

To my understanding, the issue is that Meteor is not correctly being mocked.

How can I fix this issue, so that I can use a mock of Meteor.settings in my component?


Solution

  • Instead of mocking Meteor out in a separate file, I replaced each instance of Meteor with (((Meteor || {}).settings || {}).public || {})

    For your case just include an empty dictionary w/ each level of the meteor dictionary.