开玩笑不嘲笑内部功能
我对从我的模块中调用的模拟功能有一个问题。当从测试中调用模拟功能时,它可以按预期工作。但是,当从库中调用相同的模拟函数时,它将调用实际函数。
我已经尝试了jest.mock('./ myModule',()=> {})
方法,enableAutomock
也是如此,但结果相同。
我觉得我在其他项目中没有遇到这个问题,但是已经浏览了我的玩笑配置,并且看不到任何会影响它的东西。
我在这里想念什么?如何在模块中内部调用的模拟功能?
// myModule.js
export function foo() {
return 'foo'
}
export function bar() {
return foo()
}
// myModule.test.js
import * as myModule from './myModule';
jest.spyOn(myModule, 'foo').mockReturnValue('mock foo');
// i have also tried...
// jest.mock('./myModule', () => {
// ...(jest.requireActual('./myModule')),
// foo: jest.fn().mockReturnValue('mock foo')
// });
it('should', () => {
expect(myModule.foo()).toEqual('mock foo'); // PASS: returns 'mock foo'
expect(myModule.bar()).toEqual('mock foo'); // FAIL: returns 'foo'
});
I am having an issue with mocking functions called from within my module. when the mocked function is called from the test, it works as expected. but when that same mocked function is called from the library, it calls the actual function.
I have tried the jest.mock('./myModule', () => {})
approach, and enableAutomock
as well, but with the same results.
I feel like i have not had this problem in other projects, but have looked through my jest configuration, and don't see anything that would effect it.
what am i missing here? how can i mock functions called internally within my module?
// myModule.js
export function foo() {
return 'foo'
}
export function bar() {
return foo()
}
// myModule.test.js
import * as myModule from './myModule';
jest.spyOn(myModule, 'foo').mockReturnValue('mock foo');
// i have also tried...
// jest.mock('./myModule', () => {
// ...(jest.requireActual('./myModule')),
// foo: jest.fn().mockReturnValue('mock foo')
// });
it('should', () => {
expect(myModule.foo()).toEqual('mock foo'); // PASS: returns 'mock foo'
expect(myModule.bar()).toEqual('mock foo'); // FAIL: returns 'foo'
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现在我的笑话配置中使用 ts-jest 允许这些测试按我的预期运行
I found using
ts-jest
in my jest config, allowed these tests to run as i expected