-
Notifications
You must be signed in to change notification settings - Fork 459
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
Dynamic import support? #258
Comments
This is a new feature in TypeScript, right? I haven't tested this yet but I'm marking it as a bug. |
Yes, it is a new feature 😉 |
@screendriver I've added a test that seems to be passing Can you see if the test covers this issue? If it does, then I'm not sure why it isn't working for you. Did you test with the latest typescript version? |
Oh. You are right. In my little test repository it is working as well 😳 But: if you enable |
can you try setting (this will prevent hoisting of mocks, though) |
I've added a test for dynamic imports with If you need babel, you might have to add the right plugin and update your .babelrc (this is conjecture - I haven't tested this) |
If if set By the way: I don't use Babel in my project. There is only TypeScript (and webpack). Nothing more. |
I'm not sure what's going on. Can you do one of the following:
? |
I created for you a minimal repository where you can reproduce the error.
I added "ts-jest": {
"skipBabel": true
} and |
So when you set skipBabel to true, does it work for you? |
|
This doesn't look like a ts-jest issue. I've sent a PR to the minimal repo of a version which works fine. It's got something to do with how Maybe is imported |
on second thoughts, hang on. This might be an issue with ts-jest |
yeah, I've just tested this without ts-jest and the way If you replace the import in App.tsx with the kind you have, it'll show Maybe as undefined |
Thank you 👍 I ended up in fixing the type definition (because it was wrong. There is no default export) and disabled |
Just a little update @kulshekhar : could you try it with |
Jest needs to be fed the es5 version, either directly from typescript or via babel. That's probably what's causing this issue. It's most likely not related to dynamic imports |
Ah ok. So no matter if I have Node 8.* installed and can use almost every shiny new JavaScript language feature I still have to transpile everything down to ES5? If yes: this results in slower transpilation and runtime. If we could use ES2015 as target for example, TypeScript has to transpile almost nothing and Node could run the code almost as it was written (except the imports and type definitions) => faster transpilation, faster runtime. |
It's just the module resolution part I think. Natively, node doesn't understand 'import' |
as I said 😁 |
Hi @screendriver, @kulshekhar so if I am correct then at the moment you cannot have allowSyntheticDefaultImports: true and have dynamic import work? |
i can confirm that's correct at least as far as i've experienced things....every time i try with it enabled, jest fails...
|
@gabeweaver I actually have it working however I had to bring in react-loadable using require() syntax instead of import. |
I'm not quite sure why this doesn't work - would love a PR with a fix. |
Otherwise Jest test fails. See also: kulshekhar/ts-jest#258
++++ test failing to run cause of imports
|
I think I ran into the same problem as you guys and I made a minimal repro repo here so that the maintainers can figure out what's wrong. Steps to reproduce:
|
@screendriver does the change that worked for @huy-nguyen work for you? |
Hi @kulshekhar, unfortunately not because @huy-nguyen just disable I already have two
In my CI build I run Warning: React.createElement: type is invalid -- expected a string (for built-in
components) or a class/function (for composite components) but got:
undefined. You likely forgot to export your component from the file it's
defined in, or you might have mixed up default and named imports. because TypeScript emits wrong / no files for third party dependencies like |
Finally I could create a repository that reproduces this issue 🎉
|
This might be solved by #505 |
I upgraded to version |
Did you set the |
I enabled that now (I oversight that flag...) but the tests are now really slow (4 tests are running in 30 seconds 😳) and I'm getting different errors now: TypeError: Cannot read property 'text' of undefined
at transpileViaLanguageServer (node_modules/ts-jest/dist/transpiler.js:61:21)
at Object.transpileTypescript (node_modules/ts-jest/dist/transpiler.js:10:16)
at process (node_modules/ts-jest/dist/preprocessor.js:27:41)
at Object.process (node_modules/ts-jest/index.js:8:51) |
Yeah it's definitely slower. Thanks for trying it out. |
The issue still persists in the I've created a small repo with steps to reproduce the issue. |
@screendriver I did, but the error persisted. But then I removed the Also leaving the |
In your repo I see "module": "esnext" and no |
I did a pull request and it works now in your repository (it's a different issue as I said 😉) |
Hi i've tried out the above solution with a separate jest tsconfig however i'm still getting the same error. The only difference to add is that I'm in a mixed TS project (js enabled). Could this be the cause? |
In case this helps anyone in the future, I got this working by configuring {
"presets": [["env", {"modules": false}]],
"plugins": ["syntax-dynamic-import"],
"env": {
"test": {
"plugins": ["dynamic-import-node"]
}
}
} Note you may need to run If you don't use babel for your project normally, you will still need to add a |
Thanks @christophercurrie ! |
fixed in |
In case someone still struggles with this problem, what helped for me was changing Jest preset from |
Because
ts-jest
overridesmodule
tocommonjs
in thetsconfig.json
no tests can be written with the new dynamicimport('./myModule')
syntax. You get either'import' and 'export' may only appear at the top level
or aSyntax error
.Dynamic imports should work.
Just type
import('./myModule')
in your test or implementation. That's enough.The text was updated successfully, but these errors were encountered: