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

Allow customization of Jest: transformIgnorePatterns #2537

Closed
amandapouget opened this issue Jun 14, 2017 · 19 comments
Closed

Allow customization of Jest: transformIgnorePatterns #2537

amandapouget opened this issue Jun 14, 2017 · 19 comments
Milestone

Comments

@amandapouget
Copy link

There are a ton of issues about this, and after two hours of reading through them all, I still wasn't able to get a sense of the state of things.

We have a case where we have local libraries that are brought into node_modules, but then Jest stumbles whenever we import them, because it is not pre-processing the React components in node_modules. Transpiling our components before export is not an option because the constant transpiling every time one makes a change is a serious drag on development.

Some discussion threw out symlinks as a solution or workaround, but another developer on our team sunk many hours into that before finally getting stuck again.

There has to be a simple way to make LibraryA/MyReactComponent go into MyApp/node_modules, such that MyApp/src/MyComponent can import MyReactComponent during tests without any problems.

It would seem to be a common use case.

@gaearon
Copy link
Contributor

gaearon commented Jun 14, 2017

Could you explain more about how transformIgnorePatterns would help? Have you verified (e.g. by manually changing node_modules/react-scripts/utils/createJestConfig) that this particular option would help? What did you set it to?

Is this related to #607?

@amandapouget
Copy link
Author

amandapouget commented Jun 14, 2017 via email

@gaearon
Copy link
Contributor

gaearon commented Jun 14, 2017

Is there any chance you could share what was the issue with symlinks?

@amandapouget
Copy link
Author

amandapouget commented Jun 14, 2017 via email

@gaearon
Copy link
Contributor

gaearon commented Jun 14, 2017

I’m not entirely sure myself. Unfortunately I’m overloaded with React work right now and can’t look at this in very close future, but this feedback is helpful. We know sharing components between apps is pretty important. If you could describe your setup in more detail (how many apps, how many components they share, your folder layout) this would help for future work.

@amandapouget
Copy link
Author

amandapouget commented Jun 15, 2017 via email

@gaearon
Copy link
Contributor

gaearon commented Jun 22, 2017

I see, thanks. Again, we don't have any immediate solution to this, but I feel free to participate in #1492.

@gaearon gaearon added this to the 100.0.0 milestone Jun 22, 2017
@adrianblynch
Copy link

adrianblynch commented Sep 5, 2017

I have the same issue with an ejected app. Components in node_modules that need to be transpiled by Jest/Babel.

I ended up changing transformIgnorePatterns to:

["/node_modules/(?!(our-react-components-.*?\\.js$))"]

Now JS files in our-react-components-* folders in node_modules are being transpiled.

I understand the discussion is around a non-ejected app, I just wanted to give a solution to those that don't mind ejecting.

@SunLn
Copy link

SunLn commented Sep 6, 2017

Thanks a lot.
@adrianblynch, you really help me .

@gaearon
Copy link
Contributor

gaearon commented Jan 17, 2018

I think #1333 is the first step here. It might not be sufficient for your particular use case but we'll implement it first and can then take feedback on how to expand it.

@gaearon gaearon closed this as completed Jan 17, 2018
@michaeldewolf85
Copy link

michaeldewolf85 commented Feb 23, 2018

@gaearon This isn't the production domain so while I agree with all the methodology, why wouldn't we let developers supply their own test config if they want/need to? I'm happy to submit a patch if it's worthwhile.

@michaeldewolf85
Copy link

michaeldewolf85 commented Feb 23, 2018

As far as I can tell the alternative is that everyone has to eject if they need to develop tests that import a custom dependency that they are developing using an ES6-only npm module that is linked to npm via a git repo (or equivalent).

@Primajin
Copy link
Contributor

We make use of OpenLayers, so sadly we need to eject every project, just to be able to have transformIgnorePatterns as an option. There should be a way to pass this down.

@john-goldsmith
Copy link

john-goldsmith commented May 18, 2018

@Primajin @michaeldewolf85 @mandysimon88 It's true that the transformIgnorePatterns option is currently not available via the jest property in package.json, however, you can still tap into Jest's configuration options without ejecting using the CLI:

npm test -- --transformIgnorePatterns "node_modules/(?!(your-untranspiled-module|some-other-module)/)"

Via the docs:

Every one of Jest's Configuration options can also be specified through the CLI.

Note: CLI options take precedence over values from the Configuration.

That last bit is what tipped me off (maybe a documentation improvement @gaearon). Obviously configuring Jest via package.json doesn't work, and my jest.config.js seemed to get trumped by CRA's Jest config, so CLI args was the saving grace. Hope that helps!

@bogdosarov
Copy link

@john-goldsmith, you are my hero.

@Primajin
Copy link
Contributor

Hmm weird, I always get

● Unrecognized CLI Parameter:

  Unrecognized option "transformIgnorePatterns \"node_modules/(?!(ol|jss-global)/)\"".

  CLI Options Documentation:
  https://facebook.github.io/jest/docs/en/cli.html

and there is no such parameter on that page.

@bogdosarov
Copy link

@Primajin In my case, all works without any errors.
How I use this parameter in package.json:
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!ui-core)/\" --env=jsdom"

@shinriyo
Copy link

@bogdosarov
Did you resolve?

``
FAIL src/containers/App.test.tsx
● Test suite failed to run

/Users/me/path/node_modules/decimal.js/decimal.mjs:4789
export var Decimal = clone(DEFAULTS);
^^^^^^

SyntaxError: Unexpected token export

@LiangMingChen
Copy link

this is a awful work around, if you have 10 module, the default command is just going to get huge. You might as well declare that we allow all customization and ask everyone to bypass

@lock lock bot locked and limited conversation to collaborators Jan 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants