jest: transformIgnorePatterns causes errors for npm linked packages

Do you want to request a feature or report a bug? Bug

What is the current behavior? Running simple tests works. Running tests that import npm linked packages causes errors; e.g.,

jest sanity Couldn’t find preset “react-native” relative to directory “demo/sub/ux/node_modules/my-linked-module/node_modules/react-apollo”

(My modules have no direct dep on “react-native”, but there’s a transitive dep via “react-apollo”)

But, babel (and webpack and karma) all work fine. Also, jest works fine in the dependent module (my-linked-module).

babel-node src/web/sanity.test.js OK

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

Create a dependency on another module via npm link. Import that module in a test (ES6 syntax): Add: “transformIgnorePatterns”: [ “/!node_modules\/my-linked-module/” ] to package.json

What is the expected behavior? It should compile OK

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system. jest: v19.0.2 node: v7.5.0 OSX 10.2

package.json “jest”: { “verbose”: true, “transformIgnorePatterns”: [ “/!node_modules\/my-linked-module/” ] },

.babelrc { “presets”: [ “es2015”, “stage-0”, “react” ],

“env”: { “test”: { “plugins”: [“transform-es2015-modules-commonjs”] } } }

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16

Most upvoted comments

What about this - it solved mine : .babelrc

{
  "presets": ["react-native"]
}

package.json

{
  "name": "reportApp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.2.0",
    "react-native": "0.53.0",
    "react-navigation": "^1.0.0"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-jest": "^22.2.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react-native": "^4.0.0",
    "jest": "^22.2.1",
    "jest-react-native": "^18.0.0",
    "react-test-renderer": "^16.2.0",
    "reactotron-react-native": "^1.14.0"
  },
  "jest": {
    "preset": "react-native",
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-navigation)"
    ]
  }
}

@thymikee I debugged Jest and found the cause. transformIgnorePatterns works fine, the problem appears because ScriptTransformer uses a local babelrc configuration file. Main project is aware of Jest, so I have the following in babelrc: "test": { "plugins": [ "transform-es2015-modules-commonjs" ] } However scoped sub-projects know nothing about global test environment, so there is no configuration for “test”, therefore transformation doesn’t affect es6 imports. After I updated babelrc with commonjs plugin everything works fine. I don’t know if it’s intended functionality to use local babelrc instead of global?

@thymikee it seems the problem might be with bundled babel-jest. Since

Note: babel-jest is automatically installed when installing Jest and will automatically transform files if a babel configuration exists in your project.

I do not specify custom “transform” option. If transformIgnorePatterns option is mentioned but empty it tries to process all packages in node_modules, so generally transformation works, but ignores packages in “scoped” folder

@thymikee I have the same problem, and configuration you provided "node_modules/(?!react-native|my-linked-module)/" works fine, however transformation ignores scoped packages, e.g. "node_modules/(?!@myscopedpackages)" won’t be affected by transform module.