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
- fix: jest can't find babel.config.js https://github.com/facebook/jest/issues/8006 — committed to gleb-lobastov/mine by gleb-lobastov 4 years ago
- Use `rootMode: upward` for so babel-jest can find our babel config https://github.com/facebook/jest/issues/8006 — committed to foxglove/studio by jtbandes 3 years ago
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
Or if you’re configuring from the
package.json
file then the following also works.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:It would be really nice if Babel moved forward soon to read its config files like ESLint.
This is my
jestBabelTransform.js
:And my jest config:
I’m having the same problem.