我在react应用程序中配置了i18 next-http-backend,如下所示:
import i18n from 'i18next'
import Backend from 'i18next-http-backend'
import detector from 'i18next-browser-languagedetector'
import { initReactI18next } from 'react-i18next'
i18n
.use(Backend)
.use(detector)
.use(initReactI18next)
.init({
backend: {
loadPath: `${process.env.PUBLIC_URL || ''}/locales/{{lng}}/{{ns}}.json`,
addPath: null
},
fallbackLng: 'en',
saveMissing: true,
interpolation: {
escapeValue: false // not needed for react as it escapes by default
}
})
export default i18n
在我的测试夹具中,我想模拟i18 n方面,为此我使用了以下样板文件:
jest.mock('react-i18next', () => ({
// this mock makes sure any components using the translate hook can use it without a warning being shown
useTranslation: () => {
return {
t: (str: string) => str,
i18n: {
changeLanguage: () => new Promise(() => {})
}
}
},
initReactI18next: {
type: '3rdParty',
init: () => {}
}
}))
测试还使用msw
来模拟HTTP端点,这表明我的测试仍然希望与i18 next的HTTP后端对话:
console.warn
[MSW] Warning: captured a request without a matching request handler:
• GET http://localhost/locales/en/translation.json
我怎样才能正确地模拟i18 next,以防止它试图与http后端对话?
1条答案
按热度按时间qnakjoqk1#
您可以通过从“i18 next-http-backend”模拟
Backend
来解决这个问题。这将阻止发出HTTP请求。