jest: babel-jest does not locate babel config when it is in root directory

🐛 Bug Report

Versions:

"babel-jest": "^24.1.0"
"jest": "^24.1.0"
"jest-cli": "^24.1.0"
"@babel/core": "^7.3.4"

Please tell us about your environment: Ubuntu

Folder structure Mono-repo. All projects in packages folder.

When this error occured?

When I moved babel.config.js to root folder out of local scope of package.

Folder structure

root
│   node_modules    
│   babel.config.js    
│   package.json    
└───packages
│       └───react-project
│                │  package.json
│                │  jest.config.js
│                │  src
│                     └───__tests__

Current behavior:

In react-project I launch command:

../../node_modules/.bin/jest -c $(pwd)/jest.config.js --rootDir .

I get an error because I suppose that babel.config.js is not found:

/packages/react-project/src/__tests__/acPlan.unit.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import PLAN from '@senove/billing-plan';
                                                                                                    ^^^^

    SyntaxError: Unexpected identifier

      at ScriptTransformer._transformAndBuildScript (../../node_modules/jest-runtime/build/ScriptTransformer.js:440:17
  • My configs

jest.config.js:

module.exports = {
  collectCoverage: true,
  collectCoverageFrom: [
    'src/**/*.{js,jsx}',
    '!**/node_modules/**',
    '!**/.js',
    '!**/__tests__/**',
    '!**/coverage/**',
  ],
  roots: ['<rootDir>/src'],
  testURL: 'http://localhost',
  transform: {
    '^.+\\.js$': 'babel-jest',
  },
  globals: {
    TYPE: 'SERVER',
  },
  coverageReporters: ['json', 'json-summary'],
  moduleFileExtensions: ['js', 'json'],
  testMatch: ['**/__tests__/?(*.)test.js'],
};

babel.config.js

module.exports = {
  comments: false,
  presets: [
    [
      '@babel/preset-env',
    ],
    [
      '@babel/preset-react',
    ],
  ],
  plugins: [
    "@babel/plugin-syntax-dynamic-import",
    ['@babel/plugin-proposal-class-properties', {loose: true}],
    'transform-remove-console',
  ],
  env: {
    node: {
      sourceMaps: 'both',
      sourceType: 'unambiguous',
      sourceFileName: 'index.js',
    },
  },
  ignore: ['node_modules'],
};

Additional question:

Is it possible to use one jest.config.js for all the packages’ tests’ the same way like babel.config.js. When executing tests search for jest.config.js UPROOT till it founds it?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 30
  • Comments: 23 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Hi, you can specify path in the configuration “babel-jest” like this

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

Weighing in late, but with a suggestion similar to @morgs32, you can add the following to your jest.config.js

module.exports {
  ...config,
  transform: {
    '^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { rootMode: 'upward' }],
  },
}

Or if you’re configuring from the package.json file then the following also works.

"jest": {
  "transform": {
    "^.+\\.(js|jsx|ts|tsx)$": ["babel-jest", { "rootMode": "upward" }],
  }
}

This removes the need for creating an extra file.

This was driving me crazy until I finally realized that Jest was just plain ignoring babel.config.js at the root level.

@icopp: I’m using an ugly hack now: babel.config.js in the current directory that imports the one from the parent:

module.exports = require('../babel.config.js');

It would be really nice if Babel moved forward soon to read its config files like ESLint.

This is my jestBabelTransform.js:

const babelJest = require('babel-jest');

module.exports = babelJest.createTransformer({
  rootMode: 'upward'
});

And my jest config:

  ...
  transform: {
    '^.+\\.(js|jsx|ts|tsx)$': path.resolve(__dirname, './jestBabelTransform.js'),
    ...
  }

I’m having the same problem.