Skip to content

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

Closed
@mindaugasnakrosis

Description

@mindaugasnakrosis

🐛 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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions