react-i18next: Using hooks breaks jest tests

Describe the bug Using hooks as in the provided example https://github.com/i18next/react-i18next/tree/master/example/react-hooks breaks jest tests with: Jest encountered an unexpected token

Details:

    /home/azureuser/Downloads/react-i18next-master/example/react-hooks/node_modules/react-i18next/hooks.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export {

Occurs in react-i18next version

    "name": "react-i18next",
    "version": "8.3.8"

To Reproduce

Clone the repo and run tests in the hooks example:

1.cd example/react-hooks 2.npm test

Expected behaviour Tests can be run using hooks.

The error description thrown provides a solution, including in Jest configuration an exception to allow the offending file to be processed by babel.

To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.

The problem is that using create-react-app the transformIgnorePatterns option is not everridable.

Context

I’m starting a new long term project from scratch and want to start using hooks from the begining. Is there any workaround so I can write tests using hooks, while next version with full support for hooks is available?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

Just got hit by this today 😅

Another alternative of temporary jest test using the hooks variant is jest manual mock.

// src/__mocks__/react-i18next/hooks.js
const mocked = require('react-i18next/dist/commonjs/hooks');
module.exports = mocked;

This way, you can still use react-i18next/hooks in your code, but in tests jest will “mock” it by returning the commonjs module version