这是我的函数和测试的简化版本。虽然我嘲笑了 useTranslation 我收到以下错误:
您正在传递一个未定义的模块!请检查您的对象
传递给 i18next.use()
7 | i18n
8 | .use(Backend)
> 9 | .use(initReactI18next)
我如何正确模拟以消除此错误?
import React from 'react'
import { useTranslation } from 'react-i18next'
import * as Constants from 'constants'
import MyComponent from 'components'
const Dashboard = () => {
const { t } = useTranslation('dashboard')
return (
<div> Dashboard
<MyComponent name={t(Constants.MYCOMPONENT)}/>
</div>
)
}
export default Dashboard
jest.mock('react-i18next', () => ({
useTranslation: () => ({ t: (key) => key })
}))
it('Render Dashboard without crash', () => {
const div = document.createElement('div')
ReactDOM.render(<Dashboard/>, div)
})
我可以让它为我们的设置工作的唯一方法如下:
jest.mock("react-i18next", () => ({
initReactI18next: { type: "3rdParty", init: jest.fn() },
useTranslation: () => ({ t: (key: string) => key }),
Trans: ({ children }: { children: React.ReactNode }) => children,
}));
With initReactI18next: { type: "3rdParty", init: jest.fn() },
正如所提到的,这是拼图的最后一块here https://github.com/i18next/i18next/issues/1426#issuecomment-828656983
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)