我有一个 react 项目,我已经包含了 i18next = 15.0.4 和 react-i18next = 10.2.0 依赖项。我已经创建了一个使用 react-i18next 初始化 i18next 的模块,并且我正在尝试使用 Jest 对该代码进行单元测试。
我尝试导入初始化 i18next 的 i18n 模块并使用 jest 对其进行单元测试。
这是我的 i18n.ts 模块
import i18next from "i18next";
import { initReactI18next } from "react-i18next";
const config = {
resources: {
en: {
static: {},
},
},
fallbackLng: "en",
defaultNS: "static",
ns: "static",
interpolation: {
prefix: "[",
suffix: "]",
},
};
i18next.use(initReactI18next).init(config);
export default i18next;
我试图从我的测试代码中调用它(i18n.test.ts):
import i18n from "./i18n";
describe("i18n", () => {
it("should work fine", () => {
const strings = {
key: "value",
};
i18n.addResourceBundle("en", "static", strings, true, true);
});
});
我希望这个测试能够通过,但是我得到了以下错误:
TypeError: Cannot read property 'type' of undefined
at I18n.use (node_modules/i18next/dist/commonjs/i18next.js:257:18)
这基本上指出了 i18next.js 中的这段代码
value: function use(module) {
if (module.type === 'backend') {
this.modules.backend = module;
}
我该如何解决这个问题?