ts files in node_modules is not transformed
See original GitHub issueMy 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:
- Created 4 years ago
- Comments:23
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think in this case, the ts files in
node_modules
shouldn’t be transformed. Maybe can trytransformIgnorePatterns
.For example:
transformIgnorePatterns: [ 'node_modules/(?!lodash-es/.*)', ],
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:The 2nd there are only 2 files compiled by ts:
It looks quite strange that:
dependency\index.ts
is compileddependency\imported-module.ts
is compiledI’m not so experience with this error so I don’t have a clue yet
Updated:
preserveSymlinks
inwith-dependency\tsconfig.json
solved the issue, I found it here. Still not understand why that option helps.ts-jest
actually tries to compile physical locationdependency\index.ts
, it doesn’t compilenode_modules\dependency\index.ts
so I’d say your error isn’t about ts files innode_modules
not transformed but it is something else andpreserveSymlinks
seems to play a role in this relation.