Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldn't find preset "module:metro-react-native-babel-preset" relative to directory

I'm trying to run my test suite but I am getting the following error:

Couldn't find preset "module:metro-react-native-babel-preset" relative to directory "/Users/dan/Sites/X"

      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
          at Array.map (<anonymous>)
      at OptionManager.resolvePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
      at OptionManager.mergePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
      at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
      at OptionManager.init (node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
      at File.initOptions (node_modules/babel-core/lib/transformation/file/index.js:212:65)
      at File.initOptions (node_modules/ts-jest/dist/util/hacks.js:23:43)
      at new File (node_modules/babel-core/lib/transformation/file/index.js:135:24)
      at Pipeline.transform (node_modules/babel-core/lib/transformation/pipeline.js:46:16)

Here's my jest config in package.json:

"jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json"
    ],
    "transform": {
      "^.+\\.(js)$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
      "\\.(ts|ts)x?$": "ts-jest"
    },
    "testRegex": "/src/.*.spec.(js|ts|tsx)?$",
    "globals": {
      "ts-jest": {
        "useBabelrc": true
      }
    }
  }

If needed, here's the contents of babelrc

{
  "presets": ["module:metro-react-native-babel-preset"],
  "plugins": [
    [
      "module-resolver",
      {
        "alias": {
          "~": "./src"
        }
      }
    ]
  ]
}
like image 559
Dan Avatar asked Jan 09 '19 09:01

Dan


1 Answers

This issue remains open at the time of writing.

Users seem to have mixed results with the fixes, but these two worked for me:

Option 1: Change .babelrc to .babelrc.js

Simply rename .babelrc to .babelrc.js or babel.config.js.

Don't forget to add module.exports = to the beginning of the file.

This fixed my jest issue but broke other eslint plugins for me.

Option 2: Add transform to Jest config

For option 2, I think it works around the issue by manually invoking the react-native preprocessor on all javascript files.

transform: { '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js' }

If you don't have a separate Jest config file, you can add it to your package.json like...

{
  "jest": {
    "transform": { 
      "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js" 
    }
  }
}
like image 87
jchook Avatar answered Sep 28 '22 06:09

jchook