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
I think in this case, the ts files in
node_modulesshouldn’t be transformed. Maybe can trytransformIgnorePatterns.For example:
transformIgnorePatterns: [ 'node_modules/(?!lodash-es/.*)', ],If you set target to
commonjsintsconfigand clear jest cache, it should work.FYI, I did a quick debug, coming out that it failed when ts compiled file
dependency/index.tswhen 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.tsis compileddependency\imported-module.tsis compiledI’m not so experience with this error so I don’t have a clue yet
Updated:
preserveSymlinksinwith-dependency\tsconfig.jsonsolved the issue, I found it here. Still not understand why that option helps.ts-jestactually tries to compile physical locationdependency\index.ts, it doesn’t compilenode_modules\dependency\index.tsso I’d say your error isn’t about ts files innode_modulesnot transformed but it is something else andpreserveSymlinksseems to play a role in this relation.thank you for you repo. Do you run test inside your
with dependencyfolder ? I can’t rundocker buildin my local machine btw.