开玩笑嘲笑一个对象 [英] Jest mock an object
本文介绍了开玩笑嘲笑一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要模拟对象config.js
,而不是像正常情况那样模拟函数.我有-
I need to mock an object, config.js
, rather than mocking a function as normal. I have -
//config.js .
export default {
foo: 'bar'
}
我已经尝试过-
import config from './config';
jest.mock('./config');
config.mockReturnValue({
foo: 'zed'
})
还-
import config from './config';
jest.mock('./config');
config.mockImplentation(() => ({
foo: 'zed'
}));
但是它没有嘲笑任何东西,并且我正在正常获取配置文件.
But its not mocking anything, and I am getting the config file as normal.
我在做什么错了?
推荐答案
它只是一个返回对象而不是函数的模拟
Its just a mock that returns the object instead of a function
jest.mock('./config', () => ({ foo: 'zed' }))
或
import config from './config';
config.foo = 'zed'
您遇到的问题是,它仅适用于返回函数的模块
The problem with you approach is that it would only work for modules that return functions
这篇关于开玩笑嘲笑一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文