Search code examples
javascriptecmascript-6jestjsbabeljscommonjs

Jest fails when imports files from repository using import/export default syntax


I'm using some files from another repository which is written in es6 syntax.

The current project also is written with es6 syntax, but Babel is configured so tests don't fail on the imports. but when it tries to import file from that repository it fails. When I rewrite es6 imports and exports into CommonJS require syntax tests does not fail.

Do I need some additional configuration of Jest or Babel?


Solution

  • Yes, you just need some additional configuration of Jest.

    By default Jest does not transform anything in node_modules.

    That behavior is controlled by the configuration option transformIgnorePatterns which defaults to["/node_modules/"].

    If there is code within node_modules that needs to be transformed then you can whitelist those modules by modifying transformIgnorePatterns in your Jest config:

    "transformIgnorePatterns": [
      "node_modules/(?!(first-module-to-transform|second-module-to-transform)/)"
    ]