rn-shadow-generator icon indicating copy to clipboard operation
rn-shadow-generator copied to clipboard

[SOLVED] "Jest encountered an unexpected token" when importing `ShadowView`

Open caiangums opened this issue 4 years ago • 2 comments

Facing an issue using the library with a non-TS project and testing with Jest caused me an issue with this library: Jest encountered an unexpected token. This happened when importing the ShadowView component

import { ShadowView } from "@dotmind/rn-shadow-generator"

If the maintainers consider this valid, it could be evolved to a specific session inside the documentation.

Why it happens

It is just a supposition but as this library is probably shipped without pre-compiling into "native" JS, it still has some references to new JS or even TS.

How to solve

Jest allows a config file (jest.config.js) that it is possible to pass your own configs. By default, Jest doesn't transform or change anything from node_modules/ dir. It is possible to change that and pass that with the option transformIgnorePatterns into the jest.config.js file:

module.exports = {
  // ...
  transformIgnorePatterns: [
     "node_modules/(?!@dotmind)"
  ],
  // ...
};

Those options allow a list of Regular Expressions passed as Strings and that's why the ?! should be placed at start of the desired string to be ignored. If it is needed to add another library to be ignored such as react-navigation it is possible (and recommended) to inform as a string: "node_modules/(?!@dotmind|@react-navigation)"

Hope that helps someone else! 😄

Some references

caiangums avatar Jul 12 '21 15:07 caiangums

Hey @caiangums 👋🏼 Thanks to share your solved issue.

I invite you to make a pull request to fix it. In the same time we will investigate what Regex we have to add to transformIgnorePatterns to ignore some RN libraries.

Have a nice day ☀️ Cheers

anthlasserre avatar Jul 12 '21 15:07 anthlasserre

Thanks, @anthlasserre! 😄

It is actually not an issue with the lib but with the usage and testing with Jest. Maybe it is some edge-case from the project that I'm working on and I'm just documenting it here for someone in the future who could have this as a reference 👏

caiangums avatar Jul 12 '21 15:07 caiangums