[英]Mocking node_module with jest
我在嘲笑rfx-core
節點模塊。
我在路徑中創建模擬文件: <rootDir>/__mocks__/rfx-core.js
import core from 'rfx-core';
jest.genMockFromModule('core');
module.exports = core;
我收到一個錯誤: Cannot find module 'core' from 'rfx-core.js'
來自代碼行jest.genMockFromModule('core');
不要導入您正在jest.genMockFromModule()
的模塊,只需在模塊名稱上調用jest.genMockFromModule()
:
module.exports = jest.genMockFromModule('rfx-core');
如果您不需要復雜的模擬實現,則可以避免編寫手動模擬。 只需添加jest.mock('rfx-core');
在您的測試文件中,所有rfx-core
方法都將替換為模擬函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.