Jest: transformIgnorePatterns won't exclude symlinked modules

Created on 18 Dec 2017  路  11Comments  路  Source: facebook/jest

What is the current behavior?

While trying to ignore node_modules except modules that are symlinked, jest or babel-jest won't transpile this modules as well.

* steps to reproduce *

I've created repo to reproduce and steps are inside : https://github.com/jony89/jest-symlinked-pkgs-repro

  1. create packages : pkg1, pkg2.
  2. make pkg1 to be dependent on pkg2 using symlinking.
  3. use es6 feature in pkg2 (import...)

use the following jest.config.js in pkg1 :

  "transform": { 
    "^.+\\.js?$": "babel-jest"
  },
  "collectCoverageFrom": ["src/**/*.{js}"],
  "transformIgnorePatterns": [
    "/node_modules/(?!@pkg2)"
  ],
  "setupTestFrameworkScriptFile": "./node_modules/jest-enzyme/lib/index.js",
  "unmockedModulePathPatterns": [
    "<rootDir>/node_modules/react",
    "<rootDir>/node_modules/react-dom"
  ],
  "moduleNameMapper": {
    "\\.(gif)$": "<rootDir>/__mocks__/imgMock.js"
  }
}

and add the .babelrc file

{
  "presets": [
    ["env", { "modules": false }],
    "react",
    "flow"
  ],
  "plugins": [
    "transform-class-properties",
    "transform-object-rest-spread",
    "flow-react-proptypes"
  ],
  "env": {
    "test": {
      "presets": [
        ["env"],
        "react",
        "flow"
      ],
      "plugins": [
        "transform-class-properties",
        "transform-object-rest-spread",
        "flow-react-proptypes",
        "transform-es2015-modules-commonjs"
      ]
    }
  }
}

* steps to reproduce *

just try to run jest for any simple test that uses these to pkgs

What is the expected behavior?

pkg2 should be transpiled

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

npm : 5.5.1
node : 8.4.1
jest : 21.3.0-beta.10

Confirmed Help Wanted

Most helpful comment

I've debugged it and the code was correct.

It seems that pkg2 must hold .babelrc as well in order to be transformed with babel.

after adding .babelrc to pkg2 it works well, seems like the correct behavior(?)

All 11 comments

Can you try this with the stable jest 22? There have been symlink fixes there.

If it still fails, can you create a repository with your repro? It's a bit easier to work with. Thanks!

Hey,

jest 22 does not solve this issue.

I've created repo to reproduce and steps are inside : https://github.com/jony89/jest-symlinked-pkgs-repro

@SimenB can you give some pointers on what areas of the codebase I should look at if I wanted to fix this? I've used Jest, but I've never dug into its internals.

I can start dig into this also with some guidance ?

I've debugged it and the code was correct.

It seems that pkg2 must hold .babelrc as well in order to be transformed with babel.

after adding .babelrc to pkg2 it works well, seems like the correct behavior(?)

I'm still experiencing this issue. pkg1 and pkg2 both have a .babelrc at the root.

I also have this in my jest pkg1 jest config:

"transformIgnorePatterns": [
    "/node_modules/(?!@pkg2)"
  ],

pkg2 will not compile, though.

    /Users/git/.../packages/pkg2/dist/keygen.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export var foo = 'bar';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token export

Found my solution in https://github.com/facebook/jest/issues/6229. I'm using Babel 7 and that has an impact on things.

I just ran into this issue with symlinks, and found that the issue was my babel config from my current directory didn't apply to the symlinked directory (because the file is passed to babel with its "real" path not its symlinked path)

Placing a copy of my .babelrc (Babel 6) beneath node_modules in the directory it was being symlinked from resolved the issue.

So I think the fix here is to pass the local path to babel and not expand the symlink.

@benmccormick It's been long time since your previous answer. Did you upgrade to use Babel 7 now? If yes, how did you tackle the symlink problem? Thanks.

@gmichaeljaison I've switched jobs since then and don't really remember how this played out, sorry.

Was this page helpful?
0 / 5 - 0 ratings