Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts files in node_modules is not transformed #1294

Closed
jo32 opened this issue Nov 21, 2019 · 23 comments · Fixed by #1385
Closed

ts files in node_modules is not transformed #1294

jo32 opened this issue Nov 21, 2019 · 23 comments · Fixed by #1385

Comments

@jo32
Copy link

jo32 commented Nov 21, 2019

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
        ]
    }
}
@ahnpnl
Copy link
Collaborator

ahnpnl commented Nov 21, 2019

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

@jo32
Copy link
Author

jo32 commented Nov 21, 2019

If you set target to commonjs in tsconfig and 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.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Nov 21, 2019

sorry my bad, module: commonjs

@jo32
Copy link
Author

jo32 commented Nov 21, 2019

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

@ahnpnl
Copy link
Collaborator

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

@ahnpnl
Copy link
Collaborator

ahnpnl commented Dec 8, 2019

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 👍

@sparkinson
Copy link

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

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 23, 2020

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.

@sparkinson
Copy link

sparkinson commented Jan 23, 2020

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.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 23, 2020

There seems to be an invalid config, I got error from jest

$ npm run test

> with-dependency@0.0.1 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! with-dependency@0.0.1 test: `jest '(/test/.+\.spec)\.ts$'`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the with-dependency@0.0.1 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

@sparkinson
Copy link

ok, weird, that worked on mine and in the docker image. I have pushed a change that may fix it for you.

@sparkinson
Copy link

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

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 23, 2020

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

@sparkinson
Copy link

Yes, that is the error I saw. Nice one. Sorry for the issue with getting the test running

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 23, 2020

is your repo is a monorepo type ?

@sparkinson
Copy link

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://git@bitbucket.org/{user}/{repo}.git" syntax in the package.json to install the library directly from bitbucket

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 23, 2020

if your real repo is also monorepo, please take a look at #1343 . At the moment ts-jest doesn't play well with monorepo...

@sparkinson
Copy link

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.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 23, 2020

oh, no that is just FYI, I've seen this error occuring some other places as well, #1289 probably related

@sparkinson
Copy link

ok, great. Let me know if there is anything else I can do to help.

@ahnpnl
Copy link
Collaborator

ahnpnl commented 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 Unable to require .d.ts file on Unix only #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.

@sparkinson
Copy link

sparkinson commented Jan 23, 2020

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.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 23, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants