Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An error occurs every time i import 'react-icons/fa'. #707

Closed
nabi-chan opened this issue Jun 8, 2020 · 4 comments
Closed

An error occurs every time i import 'react-icons/fa'. #707

nabi-chan opened this issue Jun 8, 2020 · 4 comments

Comments

@nabi-chan
Copy link

  • @testing-library/react version: 9.3.2
  • Testing Framework and version:
    • @testing-library/jest-dom : 4.2.4
    • jest : 24.9.0
  • DOM Environment: (Generated CRA v3)
    • node : 12.16.2
    • react : 16.13.1

What you did:

add line import { ... } from react-icons/fa

What happened:

An error was indicated in the terminal as below.

FAIL  src/Jest.test.jsx
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    .../node_modules/react-icons/all.js:2
    export * from './fa';
    ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 3 | import { MdMail, MdLock } from 'react-icons/all'
        | ^

Problem description:

Currently, I am practicing jest with CRA, and the error is showed only when importing 'react-icons/fa'.
However, when the list of other icons was called up, it passed the test normally without any errors.

@MatanBobi
Copy link
Member

MatanBobi commented Jun 10, 2020

The code doesn't really match your description since your saying that you're importing from react-icons/fa but in the code you're importing from react-icons/all (importing from react-icons/fa actually works)..
This happens because react-icons is shipped as es2015 modules and jest doesn't support this yet.
Here are some references:
jestjs/jest#4842
jestjs/jest#2081
jestjs/jest#2550

To make it work you need to edit your jest config:

const esModules = ['react-icons'].join('|');
module.exports = {
  //...
    transformIgnorePatterns: [`/node_modules/(?!${esModules})`],
  // ...
};

This one isn't related to RTL but to jest :)

@kentcdodds
Copy link
Member

Thanks @MatanBobi!

@kentcdodds
Copy link
Member

Hi @MatanBobi, added you as a triager thanks to all your contributions in the issues. Keep it up! Thank you :)

@MatanBobi
Copy link
Member

Hi @MatanBobi, added you as a triager thanks to all your contributions in the issues. Keep it up! Thank you :)

Thanks @kentcdodds, I appreciate it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants