[英]Mocking node_module with jest
I am mocking rfx-core
node module.我在嘲笑
rfx-core
节点模块。
I create mock file in path: <rootDir>/__mocks__/rfx-core.js
我在路径中创建模拟文件:
<rootDir>/__mocks__/rfx-core.js
import core from 'rfx-core';
jest.genMockFromModule('core');
module.exports = core;
I get an error: Cannot find module 'core' from 'rfx-core.js'
我收到一个错误:
Cannot find module 'core' from 'rfx-core.js'
from code line jest.genMockFromModule('core');
来自代码行
jest.genMockFromModule('core');
不要导入您正在jest.genMockFromModule()
的模块,只需在模块名称上调用jest.genMockFromModule()
:
module.exports = jest.genMockFromModule('rfx-core');
If you don't need complex mock implementation you can avoid writing a manual mock.如果您不需要复杂的模拟实现,则可以避免编写手动模拟。 Just add
jest.mock('rfx-core');
只需添加
jest.mock('rfx-core');
in your test file and all the rfx-core
methods will be replaced with mock functions.在您的测试文件中,所有
rfx-core
方法都将替换为模拟函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.