由于上述问题,我的单元测试失败了。
//String.ts
import * as i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import BrowserLanguageDetector from 'i18next-browser-languagedetector'
import Backend from 'i18next-http-backend'
import languageMap from '@katal/localization/webpack-loader!'
i18n
.use(initReactI18next)
.use(BrowserLanguageDetector)
.use(Backend)
.init({
fallbackLng: 'en-US',
load: 'currentOnly',
detection: {
order: ['sessionStorage', 'localStorage', 'querystring', 'navigator'],
lookupQuerystring: 'locale',
lookupLocalStorage: 'locale',
lookupSessionStorage: 'locale',
caches: [],
},
backend: {
loadPath: (localeList: string[]) => languageMap[localeList[0]],
},
interpolation: {
escapeValue: false,
},
react: {
useSuspense: false,
},
})
我嘲笑这一点的方式是:
const React = require('react')
const reactI18next = require('react-i18next')
module.exports = {
...reactI18next,
// this mock makes sure any components using the translate HoC or useTranslation hook receive the t function as a prop
useTranslation: (...args) => ({
...reactI18next.useTranslation(...args),
ready: true,
t: (tr) => tr,
}),
}
我将其导入到我的 App.tsx 中
import './utils/Strings'
我检查的其他一些链接是these https://stackoverflow.com/questions/62849265/error-with-i18n-error-you-are-passing-an-undefined-module-please-check-the-ob and these https://stackoverflow.com/questions/63996548/how-to-properly-mock-i18next但它们都不起作用。
任何有关解决方法的帮助都会受到赞赏