Mar*_*ter 4 reactjs jestjs enzyme react-i18next
这是我的函数和测试的简化版本。虽然我嘲笑了 useTranslation 我收到以下错误:
您正在传递一个未定义的模块!请检查您传递给 i18next.use() 的对象
Run Code Online (Sandbox Code Playgroud)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
Run Code Online (Sandbox Code Playgroud)
jest.mock('react-i18next', () => ({
useTranslation: () => ({ t: (key) => key })
}))
it('Render Dashboard without crash', () => {
const div = document.createElement('div')
ReactDOM.render(<Dashboard/>, div)
})
Run Code Online (Sandbox Code Playgroud)
我可以让它为我们的设置工作的唯一方法如下:
jest.mock("react-i18next", () => ({
initReactI18next: { type: "3rdParty", init: jest.fn() },
useTranslation: () => ({ t: (key: string) => key }),
Trans: ({ children }: { children: React.ReactNode }) => children,
}));
Run Code Online (Sandbox Code Playgroud)
和initReactI18next: { type: "3rdParty", init: jest.fn() },
作为拼图的最后一块,如此处所引用
| 归档时间: |
|
| 查看次数: |
2774 次 |
| 最近记录: |