react-i18next: You are passing an undefined module! Please check the object you are passing to i18next.use()

🐛 Bug Report

I followed the usage example files i18n with jest. I created the mock file, included in moduleNameMapper, but when I go to test a component that uses i18n, I get this error: You are passing an undefined module! Please check the object you are passing to i18next.use()

Captura de Tela 2022-10-27 às 12 31 14

To Reproduce

/src/test/__mocks__/react-i18next.js:

const React = require('react');
const reactI18next = require('react-i18next');

const hasChildren = node => node && (node.children || (node.props && node.props.children));

const getChildren = node =>
  node && node.children ? node.children : node.props && node.props.children;

const renderNodes = reactNodes => {
  if (typeof reactNodes === 'string') {
    return reactNodes;
  }

  return Object.keys(reactNodes).map((key, i) => {
    const child = reactNodes[key];
    const isElement = React.isValidElement(child);

    if (typeof child === 'string') {
      return child;
    }
    if (hasChildren(child)) {
      const inner = renderNodes(getChildren(child));
      return React.cloneElement(child, { ...child.props, key: i }, inner);
    }
    if (typeof child === 'object' && !isElement) {
      return Object.keys(child).reduce((str, childKey) => `${str}${child[childKey]}`, '');
    }

    return child;
  });
};

const useMock = [k => k, {}];
useMock.t = k => k;
useMock.i18n = {};

module.exports = {
  // this mock makes sure any components using the translate HoC receive the t function as a prop
  withTranslation: () => Component => props => <Component t={k => k} {...props} />,
  Trans: ({ children }) =>
    Array.isArray(children) ? renderNodes(children) : renderNodes([children]),
  Translation: ({ children }) => children(k => k, { i18n: {} }),
  useTranslation: () => useMock,

  // mock if needed
  I18nextProvider: reactI18next.I18nextProvider,
  initReactI18next: reactI18next.initReactI18next,
  setDefaults: reactI18next.setDefaults,
  getDefaults: reactI18next.getDefaults,
  setI18n: reactI18next.setI18n,
  getI18n: reactI18next.getI18n,
};

jest.config.js

moduleNameMapper: {
    'react-i18next': '<rootDir>/src/test/__mocks__/react-i18next.js',
  },

Your Environment

  • node: v16.14.2
  • i18next version: ^21.6.16
  • os: Mac
  • react: 18.1.0
  • next: 12.1.6

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20 (9 by maintainers)

Most upvoted comments

I think I arrived a little bit late but @lucasfelixc if you have something like this in your tests:

jest.mock('react-i18next', () => ({
    useTranslation: () => ({ t: (key: string | string[]) => key }),
    Trans: ({ i18nKey }: { i18nKey: string }) => i18nKey,
}));

you should remove it. I was having this problem and just realized that was the mock that was breaking the things.

I solve this problem adding initReactI18next in my jest.mock in top of my suit test file.

jest.mock('react-i18next', () => ({
  useTranslation: jest.fn(),
  initReactI18next: {
    type: '3rdParty',
    init: (i18next) => i18next,
  },
}));

can you console.log initReactI18next