Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest can't transpile node_modules

I am having the following error when running Jest

SyntaxError: node_modules/my_styleguide/src/components/blocks/SegmentSetting.jsx: Unexpected token (46:10)
  44 |         ) {
  45 |         return (
> 46 |           <p className="SettingDescr">{self.props.description}</p>
     |           ^
  47 |         )
  48 |       }
  49 |     }

The node_modules dependency is still in its es6 format. Jest doesn't seem to provide the option to transpile your node_modules, as it does with your actual app. Instead, jest ignores the node_modules folder.

The .bashrc file looks ok to me:

{
  "presets": ["es2015", "react", "stage-0"]
}

How do you make Jest transpile your node_modules too? It would be the equivalent of the "--ignore false" flag that we have in mocha.

like image 268
Alberto Centelles Avatar asked Sep 15 '26 15:09

Alberto Centelles


1 Answers

I would mark this as an answer rather than the answer.

I had a similar issue, multiple components in node_modules in ES2015 and in need of transpilation.

I updated transformIgnorePatterns to:

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

The negative look-ahead for modules in our-react-components-* folders mean Jest (and therefore Babel) will find and transpile our shared components.

This was in an ejected Create React App project with Jest 20.0.4.

like image 160
Adrian Lynch Avatar answered Sep 17 '26 12:09

Adrian Lynch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!