在 JEST 中嘲笑 i18n 的 useTranslation 不起作用

Rki*_*g14 6 typescript i18next jestjs react-i18next ts-jest

我的 React 组件使用 i18next 的翻译,我正在尝试使用 JEST 为其创建测试。但是,没有任何内容被翻译,我尝试模拟下面的 useTranslation 函数:

const useMock : any = [(k: any) => k, {}];
useMock.t = (k: any) => k;
useMock.i18n = {};

jest.mock('react-i18next', () => ({
  // this mock makes sure any components using the translate HoC receive the t function as a prop
  /* tslint:disable-next-line:variable-name */
  useTranslation: () => useMock,
}));
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Iur*_*ira 3

创建一个模拟文件:__mocks__/react-i18next.useTranslation.js内容如下:

module.exports = () => {
  return () => ({
    t: key => key
  })
}
Run Code Online (Sandbox Code Playgroud)