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
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.
I do this exact thing!
meteor-jest-stubs
per the instructions in the repo README.__mocks__/meteor/meteor.js
to mock the Meteor module.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