如何测试使用 react-i18next 的 react-native 组件?

How to test react-native components which are using react-i18next?

我正在尝试使用 jest 开始测试已建立的 RN 应用程序。大多数组件都包含在 react-i18next 提供的 withNamespaces 中。

当我 运行 测试时我有这个错误:

 FAIL  tests/setting.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'type' of undefined

      4 | 
      5 | i18n
    > 6 |   .use(initReactI18next)
        | ^
      7 |   .init({
      8 |     fallbackLng: 'en',
      9 |     resources: {

      at I18n.use (node_modules/i18next/dist/commonjs/i18next.js:260:16)
      at Object.<anonymous> (config/i18nForTest.js:6:1)
      at Object.<anonymous> (tests/setting.test.js:5:43)

我遵循了文档 example,我所做的基本上是: 为测试制作 i18n 配置:

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';


i18n
  .use(initReactI18next)
  .init({
    fallbackLng: 'en',
    resources: {
      en: {},
      fr: {}
    },
    // have a common namespace used around the full app
    ns: ['translations'],
    defaultNS: 'translations',

    debug: true,

    interpolation: {
      escapeValue: false, // not needed for react!!
    },
  });

export default i18n;

然后写了一些测试:

import 'react-native';
import React from 'react';
import renderer from 'react-test-renderer';
import {I18nextProvider} from 'react-i18next';
import i18n from 'config/i18nForTest';
import Settings from 'containers/Settings';

it('Does Settings renders correctly?', () => {
  const tree = renderer
    .create(
      <I18nextProvider i18n={i18n}>
        <Settings t= {key => key} />
      </I18nextProvider>,
    )
    .toJSON();
  expect(tree).toMatchSnapshot();
});

你可以在package.json中找到我的笑话配置:



"jest": {
    "setupFiles": ["<rootDir>/jest.setup.js"],
    "preset": "react-native",
    "testMatch": [
      "<rootDir>/tests/**/*.test.js?(x)",
      "<rootDir>/src/**/*.test.js"
    ],
    "transformIgnorePatterns": ["node_modules/(?!(@react-native-community|react-navigation|react-native))"
    ],
    "transform": {
      "^.+\.(js)$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
    }
  },

谢谢!

我终于找到了解决问题的方法。 我认为文档中提供的测试示例适用于 v10,而我使用的是 v9。 所以我修改了 i18n 配置测试以使用 reactI18nextModule 所以它现在像这样:

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import { reactI18nextModule } from 'react-i18next';


 i18n
  .use(reactI18nextModule)
  .init({
    fallbackLng: 'en',
    resources: {
      en: {},
      fr: {}
    },
    // have a common namespace used around the full app
    ns: ['translations'],
    defaultNS: 'translations',

    debug: true,

    interpolation: {
      escapeValue: false, // not needed for react!!
    },
  });

export default i18n;

之后我可以像这样在测试组件中省略 i18nProvider:

import 'react-native';
import React from 'react';
import renderer from 'react-test-renderer';
import {I18nextProvider} from 'react-i18next';
import i18n from 'config/i18nForTest';
import {Settings} from 'containers/Settings'; // to get redux connected component import Setting without {}


it('Does Settings renders correctly?', () => {
  const tree = renderer
    .create(
        <Settings t={key => key} />
         )
    .toJSON();
  expect(tree).toMatchSnapshot();
});