错误:您正在传递未定义的模块!请检查您传递给 i18next.use() 的对象
由于上述问题,我的单元测试失败了。
//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'
My unit tests are failing due to the above issue.
//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,
},
})
The way I'm mocking this is :
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,
}),
}
I'm import this in my App.tsx like
import './utils/Strings'
Some of the other links I checked are these and these but none of them work.
Any help is appreciated on the workaround
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也为此苦苦挣扎了一段时间。
我的测试很好,直到我决定在组件之外的东西上使用 i18n.t() 。开玩笑不喜欢
基于这个答案我让它工作 https://github .com/i18next/i18next/issues/1426#issuecomment-828656983
这是一个适合我的解决方案:
编辑:没有看到 adrai 评论,他首先提到了这一点,但为了清楚起见,我将保留显示所有文件内容的答案
I've been struggling quite some time on this as well..
My testing was fine until i decided to use i18n.t() on something outside a component. Jest didn't like that
Based on this answer i got it working https://github.com/i18next/i18next/issues/1426#issuecomment-828656983
Here is a solution working for me:
EDIT: didn't see adrai comment, he mentionned it first, but for clarity sake i'll keep my answer that display all the file content