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

Curious behavior with --no-cache option and ts imports from node_modules #805

Closed
DorianGrey opened this issue Oct 10, 2018 · 12 comments
Closed
Labels

Comments

@DorianGrey
Copy link
Contributor

DorianGrey commented Oct 10, 2018

Issue :

Hi,

I've recently tried to update ts-jest in one of my projects from 23.1 to 23.10.
Commit: DorianGrey/vue-ts-playground@362999f
(There is a level of indirection with @vue/cli, but that should not hurt.
Everything worked fine at first glance, but then my pre-push hook kicked in and complained that it cannot import a .d.ts file:

 FAIL  tests/unit/views/todoList/todoListEntryDisplay.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 `en.ts`.

      2 | import dateTimeFormat from "./dateTimeFormat";
      3 | import messages from "./messages.json";
    > 4 | import vuetifyMessages from "vuetify/src/locale/en";
        | ^
      5 | 
      6 | export default {
      7 |   language: "en",

      at getOutput (node_modules/ts-jest/dist/compiler.js:163:23)
      at Object.compile (node_modules/ts-jest/dist/compiler.js:185:29)
      at TsJestTransformer.process (node_modules/ts-jest/dist/ts-jest-transformer.js:101:41)
      at Object.<anonymous> (src/i18n/lang-packs/en/index.ts:4:1)

It's just that the target file is a .ts file, thus, it should not expect a .d.ts file for it to be present at all. Even worse, the test executed fine when executed by my regular test:unit task.
After a bit of fiddling around, I figured out that the problem is somewhat related to the --no-cache option that is used in the pre-push hook, resp. the test:unit:ci task. This flag is not used in the other tasks (which executes fine as mentioned above). Removing that flag from the latter task also makes it work fine.
That flag is enabled since the test:unit:ci task also collects coverage information, which is known too have some issues when using the flag, esp. when used with vue components (see e.g. vuejs/vue-jest#56).

Expected behavior :

Executing test, i.e. transforming the related files, should work regardless of jest's cache being used or not.

Debug log:

Added as file attachment, since it's too long to post it directly:
ts-jest.log

Minimal repo :

https://github.com/DorianGrey/vue-ts-playground/tree/ts-jest-update-import-failure
(If that's too large, I may try to put things together in a smaller repro... though it won't be that much smaller. The affected code is quite small, actually).
The failing test can be executed via

yarn test:unit --no-cache todoListEntryDisplay

Note that the test will work fine if the --no-cache is NOT provided.

Travis build is here: https://travis-ci.org/DorianGrey/vue-ts-playground/builds/439530439 (test execution is somewhere around L700).

@huafu
Copy link
Collaborator

huafu commented Oct 10, 2018

Hi @DorianGrey thanks for reporting the issue. The "minimal repo" is just that often people cannot give a link to their repo as it is private. So it's fine with your repo as soon as we can reproduce the issue with a command line that does not run 1000 tests ;-)

Can you do this and give the result please:

yarn jest --clearCache
yarn test:unit todoListEntryDisplay

@DorianGrey
Copy link
Contributor Author

HI @huafu ,
after manually clearing jest's cache manually, the test fails with the same error that occurs when using the --no-cache flag. However, when trying to execute the test again (without clearing the cache) everything works fine... So this seems to be a cache-related issue.

@huafu
Copy link
Collaborator

huafu commented Oct 10, 2018

hmmmm. that's definitely a bug! thanks!

@huafu huafu added this to the 23.10.5 milestone Oct 10, 2018
@huafu huafu added the 🐛 Bug label Oct 10, 2018
@ahnpnl ahnpnl modified the milestones: 23.10.5, doc:next, next Nov 25, 2018
@alfaproject
Copy link

I think I've just stumbled upon this issue myself.

I have a non-test file in my __tests__ folder with some test data which is imported into the actual test files, but running the specs without cache give the exact same error as the OP:

FAIL  __tests__/event-sourcing/event-sourcing.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 `example-aggregate.ts`.

The offending line in that test file:

import { COUNTER } from './example-aggregate';

@perkrlsn
Copy link

@alfaproject Ran into this error as well. We're importing modules from another project which did not have any .d.ts files associated with them. We solved it by adding this to jest.config.js

globals: {
  'ts-jest': {
    isolatedModules: true
  },
},

@alfaproject
Copy link

I would prefer to have type checking but in my case, I'm only using local files (as you can see in the import) and all my TSConfig settings enable declarations. ):

@ArgonAlex
Copy link

I just ran into this same problem and did some debugging. The reason for this is because Typescript is specifically ignoring node_modules with the getEmitOutput() API that ts-jest is using when type checking is enabled. microsoft/TypeScript#11946

So if you still want full type checking, the solution is to specifically include the node_modules files in the includes in the tsconfig.

@prad9
Copy link

prad9 commented Apr 22, 2019

@alfaproject Ran into this error as well. We're importing modules from another project which did not have any .d.ts files associated with them. We solved it by adding this to jest.config.js

globals: {
  'ts-jest': {
    isolatedModules: true
  },
},

I had same problem too even with the imported module with d.ts files. But for some reason it worked after setting isolatedModules to true

"@types/jest": "^24.0.11",
"jest": "^24.6.0",
"ts-jest": "^24.0.2",

@bohendo
Copy link

bohendo commented Sep 6, 2019

I ran into this issue as well.

I'm running stuff in docker so cache was never persisting. Fixed that with a bind-mount and adding a cacheDirectory option to jest.config.js that maps to my local filesystem.

Still, errors out with the following message anytime I run tests:

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`.

This is apparently bc there's no cache yet and the tests seem to not get far enough along for the necessary cache to ever be created.

If I add the following line to my jest.config.js then, then tests will start running w/out error.

  "globals": { "ts-jest": { "isolatedModules": true }, },

Interestingly, once the tests pass once w this option, it can be removed later and the tests will continue passing as long as I don't delete the cache dir or run things with --no-cache.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 1, 2020

i will close this issue. If anyone still encounters this with latest jest and ts-jest, feel free to open a new issue with a minimum repo

@ahnpnl ahnpnl closed this as completed Jan 1, 2020
@mfellner
Copy link

mfellner commented Jan 2, 2020

@ahnpnl, here is a minimal repository to reproduce the issue: https://github.com/mfellner/ts-jest-issue-805-demo
Do you prefer a new issue or could you re-open this one?

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 2, 2020

Hi, I would prefer a new issue. Would you please create a new one with your repo and describe the issue ?

thinkh added a commit to datavisyn/tdp_core that referenced this issue Apr 1, 2020
With the last change CircleCI shows some errors regarding the caching, similar to
kulshekhar/ts-jest#805 (comment)

Adding the flag might solve the problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants