如何在jest中模拟Meteor.settings?

huangapple go评论62阅读模式
英文:

How would I mock out Meteor.settings with jest?

问题

Meteor.settings 在一个我正在尝试模拟的 React 类组件中被使用。在该组件的构造函数中,会访问 Meteor.settings 来获取一个 URL。我正在使用 jest 中的 moduleMapper 来模拟 Meteor.settings,如下所示:

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

export default Meteor;

在我的测试文件中,我导入了我的类组件。运行 jest 后,我收到以下错误消息:

TypeError: Cannot read property 'settings' of undefined

据我理解,问题在于 Meteor 没有正确被模拟。

我该如何解决这个问题,以便我可以在我的组件中使用 Meteor.settings 的模拟?

英文:

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?

答案1

得分: 0

我替换了每个 Meteor 实例,用(((Meteor || {}).settings || {}).public || {})代替,在一个单独的文件中模拟 Meteor。对于你的情况,只需在 Meteor 字典的每个级别包含一个空字典。

英文:

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

得分: 0

  1. 根据存储库README中的说明,包括meteor-jest-stubs
  2. 通过在__mocks__/meteor/meteor.js中包含一个文件来模拟Meteor模块的缺失/自定义部分。
import { Meteor as MockedMongo } from "meteor/meteor"

export const Meteor = {
  ...MockedMongo,
  // 然后模拟其他Meteor值,例如:
  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()
  })),
  // 等等。
}

然后jest会自动检测__mocks__目录,并在您的测试中模拟Meteor值。
https://jestjs.io/docs/manual-mocks#mocking-user-modules

英文:

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

huangapple
  • 本文由 发表于 2023年2月24日 02:05:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定