[SOLVED] "Jest encountered an unexpected token" when importing `ShadowView`
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
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
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 👏