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)
It seems that ts-jest is ignoring the file in node_modules.
My config is:
```jest.config.js
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
};
```tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"esnext",
"es2015.promise" // Or "es2015" or "es6" should work as well
]
}
}
If you set target to commonjs in tsconfig and clear jest cache, it should work.
If you set target to
commonjsintsconfigand clear jest cache, it should work.
error TS6046: Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'esnext'.
@ahnpnl It seems commonjs is not an option. And If it is, commonjs does't support circular import as far as I know.
sorry my bad, module: commonjs
sorry my bad,
module: commonjs
@ahnpnl I should have guessed that. I changed the tsconfig to be:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": [
"dom",
"esnext",
"es2015.promise" // Or "es2015" or "es6" should work as well
],
"declaration": true,
"outDir": "lib"
}
}
still no good
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/.*)',
],
Hi,
Do you still encounter the issue ? If yes, please check my latest comment. If still not working, please provide a minimal reproduce repo. Thanks 👍
Hi,
I have also been having this problem and have produced a minimal repo that demonstrates it. You can find it here: https://bitbucket.org/s_parkinson/ts-jest-bug-example
Weirdly, the tests run the second time you try to run them, so I have provided a Dockerfile to run this in a docker and make it repeatable. Across different repos I have with this issue, sometimes the second time I run the tests after installing it runs successfully, sometimes the 7-8th time it runs successfully?? which is a nightmare for CI.
If you could help with this it would be amazing. Thanks,
Sam
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.
yes, exactly. As in the Dockerfile.
cd with-dependency
npm install
npm run test
but note, when doing it on your machine, the tests may pass on the second or third run. They do on mine.
There seems to be an invalid config, I got error from jest
$ npm run test
> [email protected] test C:\Users\apham3\ts-jest-bug-example\with-dependency
> jest '(/test/.+\.spec)\.ts$'
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In C:\Users\apham3\ts-jest-bug-example\with-dependency
5 files checked.
testMatch: - 0 matches
testPathIgnorePatterns: \\node_modules\\ - 5 matches
testRegex: (\\test\\.+\.spec)\.tsx?$ - 1 match
Pattern: '(\\test\\.+\.spec)\.ts$' - 0 matches
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `jest '(/test/.+\.spec)\.ts$'`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
ok, weird, that worked on mine and in the docker image. I have pushed a change that may fix it for you.
FYI I just found that the bug is reproducible on my local machine by running node_modules/jest/bin/jest.js --clearCache before running the tests
Just fetch and pull, your change didn't work, however I can run your test by running npm run test /test/index.spec.ts. Then I ran into error
$ jest '/test/index.spec.ts' test/index.spec.ts
FAIL test/index.spec.ts
● Test suite failed to run
TypeError: Unable to require `.d.ts` file.
This is usually the result of a faulty configuration or import. Make sure there is a `.js`, `.json` or another executable extension available
alongside `index.ts`.
at getOutput (node_modules/ts-jest/dist/compiler.js:165:23)
is this the error you saw ?
--clearCache indeed will be a good way to reproduce
Yes, that is the error I saw. Nice one. Sorry for the issue with getting the test running
is your repo is a monorepo type ?
this test repo is, but not my production code.
for the production code I have a similar setup, but I am using
"repo": "git+ssh://[email protected]/{user}/{repo}.git" syntax in the package.json to install the library directly from bitbucket
if your real repo is also monorepo, please take a look at #1343 . At the moment ts-jest doesn't play well with monorepo...
Ok, so do you need a different example minimal repo where it isn't a monorepo? The same issue appears in my production code, which isn't a monorepo.
oh, no that is just FYI, I've seen this error occuring some other places as well, #1289 probably related
ok, great. Let me know if there is anything else I can do to help.
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 in with-dependency\tsconfig.json solved the issue, I found it here. Still not understand why that option helps.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.You're right. Nice one. That does fix it for the monorepo case.
Unfortunately, it does not fix it for the case where the library is not symlinked... As you might expect.
I have created a new repo that shows this: https://bitbucket.org/s_parkinson/ts-jest-bug-example-no-monorepo
I guess they are different issues.
Thanks I will take a look when I have some free time 👍
updated: I can't access the URL of your repo above @sparkinson. I think it's easier if you put your repo on github or gitlab.
Most helpful comment
I think in this case, the ts files in
node_modulesshouldn't be transformed. Maybe can trytransformIgnorePatterns.For example:
transformIgnorePatterns: [ 'node_modules/(?!lodash-es/.*)', ],