question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ts files in node_modules is not transformed

See original GitHub issue

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
        ]
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:23

github_iconTop GitHub Comments

6reactions
ahnpnlcommented, Nov 21, 2019

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/.*)', ],

1reaction
ahnpnlcommented, Jan 23, 2020

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.
Read more comments on GitHub >

github_iconTop Results From Across the Web

can't transform a folder inside node_modules with ts-jest
I'm using ts-jest compiler and jest still complains about esnext modules in node_modules folder. package versions: "@types/jest": "^23.3.12", " ...
Read more >
Ignored files | ts-node - TypeStrong · GitHub
When ts-node is used with allowJs , all non-ignored JavaScript files are transformed by ts-node. Skipping node_modules ​. By default, ts-node avoids compiling ......
Read more >
API - esbuild
There are two main API calls in esbuild's API: transform and build. ... This API call is used by the command-line interface if...
Read more >
ts-loader - npm
ts files in node_modules . You should not need to recompile .ts files there, but if you really want to, use this option....
Read more >
Documentation - Module Resolution - TypeScript
Finally, if the compiler could not resolve the module, it will log an error. ... A node_modules folder can be on the same...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found