Skip to content

Allow customization of Jest: transformIgnorePatterns #2537

Closed

Description

@amandapouget

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.

Activity

  1. gaearon commented on Jun 14, 2017

    @gaearon
    Contributor

    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?

  2. amandapouget commented on Jun 14, 2017

    @amandapouget
    Author
  3. gaearon commented on Jun 14, 2017

    @gaearon
    Contributor

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

  4. amandapouget commented on Jun 14, 2017

    @amandapouget
    Author
  5. gaearon commented on Jun 14, 2017

    @gaearon
    Contributor

    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.

  6. amandapouget commented on Jun 15, 2017

    @amandapouget
    Author
  7. gaearon commented on Jun 22, 2017

    @gaearon
    Contributor

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

  8. added this to the milestone on Jun 22, 2017
  9. adrianblynch commented on Sep 5, 2017

    @adrianblynch

    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.

  10. SunLn commented on Sep 6, 2017

    @SunLn

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

  11. gaearon commented on Jan 17, 2018

    @gaearon
    Contributor

    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.

  12. michaeldewolf85 commented on Feb 23, 2018

    @michaeldewolf85

    @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.

  13. michaeldewolf85 commented on Feb 23, 2018

    @michaeldewolf85

    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).

  14. Primajin commented on Mar 21, 2018

    @Primajin
    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.

  15. john-goldsmith commented on May 18, 2018

    @john-goldsmith

    @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!

  16. bogdosarov commented on Jul 18, 2018

    @bogdosarov

    @john-goldsmith, you are my hero.

  17. Primajin commented on Jul 18, 2018

    @Primajin
    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.

  18. bogdosarov commented on Jul 19, 2018

    @bogdosarov

    @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"

  19. shinriyo commented on Aug 23, 2018

    @shinriyo

    @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
    
  20. LiangMingChen commented on Dec 3, 2018

    @LiangMingChen

    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
    https://github.com/facebook/create-react-app/blob/49e258b4a6d04fb7e3542d7a060c6dcad4093564/packages/react-scripts/scripts/utils/createJestConfig.js#L73

  21. locked and limited conversation to collaborators on Jan 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions