我正在使用react测试库来测试我的代码,并且我已经使用i18n.addResourceBundle即时添加了一些翻译。我正在尝试对其进行测试并具有
jest.mock('i18n',()=>({ __esModule:是的, 默认值:{addResourceBundle:jest.fn()} }))
当我尝试做快照时,一直说i18n.addResourceBundle没有定义
答案 0 :(得分:0)
您在不正确地嘲笑i18next。
i18next的用法如下:
import i18next from 'i18next';
export const i18n = i18next.init({
...
// config
...
});
// somewhere else in the code
i18n.addResourceBundle();
//-^ this is an instance of i18next
这意味着您需要使用init
函数返回一个对象,该函数将返回一个addResourceBundle
的实例。
jest.mock('i18n', () => ({
__esModule: true,
default: {
init(config) {
return {
// this is the instance
addResourceBundle: jest.fn(),
};
},
},
}));