React-i18next: pass in an i18next instance by using i18nextReactModule - required action?

Created on 20 Feb 2019  路  4Comments  路  Source: i18next/react-i18next

So, when testing a component that uses the useTranslation hook, I get this warning:

react-i18next:: You will need pass in an i18next instance by using i18nextReactModule

It kinda says what I'm supposed to do, but I don't understand it. The setup of the jest suite contains the same i18n initialization code as the application (where everything works fine).

react-18next version: "10.1.1"

Reproduction
To me it looks like

  1. Create a component that uses useTranslation.
  2. Test it with your favorite testing library.

Expected behaviour
For most tests this warning does not seem to have any repercussions. Is it actually needed?

OS (please complete the following information):

  • Device: Windows 10 machine
  • Browser: pretty recent jest, very recent react

Most helpful comment

useTranslation needs an i18n instance -> so it warns about not having one.

either use i18next.use(initReactI18next) or the I18nextProvider to get "mock" down.

Details you should see in our tests in this repo and the JEST sample: https://github.com/i18next/react-i18next/tree/master/example/test-jest

All 4 comments

useTranslation needs an i18n instance -> so it warns about not having one.

either use i18next.use(initReactI18next) or the I18nextProvider to get "mock" down.

Details you should see in our tests in this repo and the JEST sample: https://github.com/i18next/react-i18next/tree/master/example/test-jest

Okay, I got round to set up the manual mock - it works fine, but I guess I'll have to watch out for breaking changes now.

@flq If you by any chance remember how you solved this, I would appreciate any additional info. I tried to make manual mock for react-i18next in __mocks__ folder adjacent to node_modules - exactly the same way as in the example project linked above, but my component still seems to use original implementation of useTranslation() when logged with useTranslation.toString() and as a result, I still see the warning.

I'm afraid this is rather a jest thing then - because for me, placing a file into __mocks__ worked. It is called react-i18next.js in my folder and it works...

Was this page helpful?
0 / 5 - 0 ratings