Hi,
I've recently tried to update ts-jest in one of my projects from 23.1 to 23.10.
Commit: https://github.com/DorianGrey/vue-ts-playground/commit/362999f603f6b3eae3bbf23cc1ba207927b1837c
(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. https://github.com/vuejs/vue-jest/issues/56).
Executing test, i.e. transforming the related files, should work regardless of jest's cache being used or not.
Added as file attachment, since it's too long to post it directly:
ts-jest.log
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).
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
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.
hmmmm. that's definitely a bug! thanks!
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';
@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 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. ):
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. https://github.com/Microsoft/TypeScript/issues/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.
@alfaproject Ran into this error as well. We're importing modules from another project which did not have any
.d.tsfiles associated with them. We solved it by adding this tojest.config.jsglobals: { '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",
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.
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, 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?
Hi, I would prefer a new issue. Would you please create a new one with your repo and describe the issue ?
Most helpful comment
@alfaproject Ran into this error as well. We're importing modules from another project which did not have any
.d.tsfiles associated with them. We solved it by adding this tojest.config.js