gpt4 book ai didi

reactjs - 如何正确模拟 i18next

转载 作者:行者123 更新时间:2023-12-04 03:55:22 24 4
gpt4 key购买 nike

这是我的函数和测试的简化版本。尽管我 mock 了 useTranslation,但我收到以下错误:

You are passing an undefined module! Please check the object you arepassing to 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,
}));
initReactI18next: { type: "3rdParty", init: jest.fn() },作为拼图的最后一块,引用 here

关于reactjs - 如何正确模拟 i18next,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63996548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com