javascript - 用于单一导入的 Jest 模拟模块
问题描述
当jest
与 ES6 模块一起使用时babel-jest
,所有的jest.mock
调用都会被提升。
假设我想模拟fs
测试类的模块,但保留其余模块的原始实现(例如我在测试期间使用的一些实用程序)。
考虑以下示例:
class UnderTest {
someFunction(){
fs.existsSync('blah');
}
}
class TestUtility {
someOtherFunction(){
fs.existsSync('blahblah');
}
}
考试:
it('Should test someFunction with mocked fs while using TestUtility'', () => {
testUtility.someOtherFunction(); // Should work as expected
underTest.someFunction(); // Should work with mock implementation of 'fs'
})
现在,人们会期望通过以下方法,该fs
模块将被模拟,UnderTest
但不会被模拟TestUtility
。
import {TestUtility} from './test-utility';
jest.mock('fs');
import {UnderTest } from './under-test';
但是,由于提升,fs
模块将被所有模块模拟(这是不希望的)。
有没有办法实现所描述的行为?
解决方案
在测试中选择退出模拟模块jest.doMock(moduleName, factory, options)
并jest.dontMock(moduleName)
应使用。
jest.doMock(模块名称,工厂,选项)
使用时
babel-jest
,对的调用mock
将自动提升到代码块的顶部。如果您想明确避免此行为,请使用此方法。
jest.dontMock(moduleName)
使用时
babel-jest
,对的调用unmock
将自动提升到代码块的顶部。如果您想明确避免此行为,请使用此方法。
所以在你的情况下,我会尝试类似
beforeEach(() => {
jest.resetModules();
});
it('Should test someFunction with mocked fs while using TestUtility'', () => {
jest.dontMock('fs');
testUtility.someOtherFunction(); // Should work as expected
jest.doMock('fs', () => {
return ... // return your fs mock implementation;
});
underTest.someFunction(); // Should work with mock implementation of 'fs'
})
推荐阅读
- wordpress - 用户报告看到旧网站,当新网站上线时
- jquery - 删除 fullpage.js 特定部分的动画
- sql - SQL Windowing Ranks 函数
- sqlite - 将列表视图项目作为新条目单独插入数据库
- python - 在 PyTorch 中使用 module.to() 移动成员张量
- javascript - 如何使用 Django 将随机生成的数字发送到用户电子邮件?
- excel - 如何在 Excel VBA 中读取 XML 自闭合标签?
- azure - 将参数从数据工厂中的 Web 活动发送到逻辑应用
- featuretools - 特征工具中是否有用于交互的内置原语?
- docker - 使用 docker image 在 Heroku 上部署 Go App + Vue.js