skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. I do this exact thing!

    1. Include meteor-jest-stubs per the instructions in the repo README.
    2. Stub out missing/custom pieces by including a file at __mocks__/meteor/meteor.js to mock the Meteor module.
    import { Meteor as MockedMongo } from "meteor/meteor"
    
    export const Meteor = {
      ...MockedMongo,
      // then stub out any other meteor values, for example:
      absoluteUrl: jest.fn().mockReturnValue("http://localhost:3000/"),
      settings: {
        public: {
          URL: "http://testing:0000",
        },
        private: {
          MY_PRIVATE_KEY: "secret"
        }
      },
      subscribe: jest.fn().mockImplementation(() => ({
        ready: jest.fn().mockReturnValue(true),
        stop: jest.fn()
      })),
      // etc.
    }
    

    Then jest picks up on the __mocks__ directory automatically and will mock out the Meteor value in your tests.
    https://jestjs.io/docs/manual-mocks#mocking-user-modules

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search