getting jest to transpile outside of src directory

Viewed 1475

I have a monorepo setup where the project running tests is trying to load a file from outside the current working directory.

Directory structure

root
 mainApp
   src...
 library
   greetings.js

Within greetings.js

export  const greetings = "hello World!"

Every time I try to import from greetings.js in my mainApp test files

//mainApp.test.js
import {greetings} from "../../greetings.js";

I get error from Jest in console

Jest encountered an unexpected token
SyntaxError: Unexpected token export

One solution in the following url is to add .babelrc to library folder https://github.com/coryhouse/react-slingshot/issues/455

I have quite a few libraries and adding .babelrc to each one isn't that clean. Is there any other / better way of achieving this.

I have even tried to add the following within setUp.js file for jest

require('babel-register')({
  plugins: ['transform-es2015-modules-commonjs'],
  presets: ['env']
});

But jest is still failing. Any help would be greatly appreciated!!

Many Thanks

2 Answers

I ran into the same issue and found this comment.

Rename .babelrc to babel.config.js and export your config instead.

babel.config.js
module.exports = {}

In my case babel-jest could not find it's configuration file, this could be fixed by configuring the transform property in the Jest configuration.

Example for setting the babel-jest configFile property explicitly for babel-jest:

transform: {
 '^.+\\.[jt]sx?$': [
   'babel-jest', { configFile: path.resolve(__dirname, 'config/babel.config.js') }
  1] 
},
Related