简体   繁体   中英

Mock module variable with Jest

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文简体   中文繁体   中英对照 版本,有任何建议请联系yoyou2525@163.com。

I've been using Jest to mock some variables, which are used in tested functions, like so:

global.SERVER_URL = 'test';
await hydrateApp();
expect(ReactDOM.hydrate).toBeCalled();

Here, the newly set value of SERVER_URL is used by hydrateApp() . However, I cannot do this with module . When I try to set module.hot , it will still be undefined, when the function is called:

global.module.hot = {
  accept: jest.fn()
};
await hydrateApp();
expect(global.module.hot.accept).toBeCalled();

In this case, module.hot is undefined inside hydrateApp() .

How can I set variable on the module object to be used by a function being tested?

No answers.You can refer to the related questions on the right.
暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM