ts-jest: ts files in node_modules is not transformed

My project imports ts files in a package in node_modules, and when I run npx jest, error happend:

FAIL  __test__/<FILENAME>.spec.ts
  ● Test suite failed to run

    /Users/<USERNAME>/Projects/<PACKAGE>/node_modules/<PATH_TO>/<FILE>.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import <NAME> from "./<PATH>";
                                                                                                    ^^^^

    SyntaxError: Unexpected identifier



      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (index.ts:4274:26)

  • sorry for pruning sensitive informations.

It seems that ts-jest is ignoring the file in node_modules.

My config is:

module.exports = {
    transform: {
        "^.+\\.tsx?$": "ts-jest"
    },
    testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(js|ts)$",
    testPathIgnorePatterns: ["/lib/", "/node_modules/"],
    moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
    modulePathIgnorePatterns: ["built"],
    collectCoverage: true
};
{
    "compilerOptions": {
        "target": "es5",
        "lib": [
            "dom",
            "esnext",
            "es2015.promise" // Or "es2015" or "es6" should work as well
        ]
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 23

Most upvoted comments

I think in this case, the ts files in node_modules shouldn’t be transformed. Maybe can try transformIgnorePatterns.

For example: transformIgnorePatterns: [ 'node_modules/(?!lodash-es/.*)', ],

If you set target to commonjs in tsconfig and clear jest cache, it should work.

FYI, I did a quick debug, coming out that it failed when ts compiled file dependency/index.ts when hitting this line. There are 3 files compiled by ts in 1st run no cache:

  • index.spec.ts
  • with-dependency\index.ts
  • dependency\index.ts

The 2nd there are only 2 files compiled by ts:

  • dependency\index.ts
  • dependency\imported-module.ts

It looks quite strange that:

  • In both runs, dependency\index.ts is compiled
  • Only 2nd run dependency\imported-module.ts is compiled

I’m not so experience with this error so I don’t have a clue yet

Updated:

  • Similar to #1289 , set preserveSymlinks in with-dependency\tsconfig.json solved the issue, I found it here. Still not understand why that option helps.
  • When debugging, I can see ts compiler inside ts-jest actually tries to compile physical location dependency\index.ts, it doesn’t compile node_modules\dependency\index.ts so I’d say your error isn’t about ts files in node_modules not transformed but it is something else and preserveSymlinks seems to play a role in this relation.

thank you for you repo. Do you run test inside your with dependency folder ? I can’t run docker build in my local machine btw.