-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Starter unit test fails when creating a Vue 3 project with TypeScript and Jest #5714
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
Comments
/cc @lmiller1990 @JessicaSachs I'm not sure but it looks like a vue-jest bug. |
Hm, I was able to reproduce this following the above steps. I then created a new project and chose not to use babel alongside TS and it worked fine. I think the problem is related to the babel transpilation running after the TS compilation step (maybe the In the past, when I saw this error (but in a different context, not a vue-cli project, but I was using jest), I had to provide the If you just want everything to work @adarean5, you can install the awesome I wonder if we can just use |
vue-cli/packages/@vue/cli-plugin-unit-jest/index.js Lines 13 to 15 in f99079c
vue-cli/packages/@vue/babel-preset-app/index.js Lines 221 to 227 in f99079c
I don't know how to debug a Jest transformer but it seems vue-jest failed to pick up this babel config. |
It does seem that way. Maybe finding the babel config is not working in the latest version of What do you think about using |
I think the bug may reside here https://github.com/vuejs/vue-jest/blob/e0cbca7920e3afca3bd333377a4e463b57631e66/lib/transformers/typescript.js#L29-L45 A babel config was found so no inline options were applied, but the project babel config did not take effect either. |
We have both Babel was required in many circumstances because of the need for modern mode, auto polyfill injection, and JSX, etc. |
Hm, I wonder why this wasn't a problem in the past. That part of I made an issue in vue-jest to also track this: vuejs/vue-jest#258 |
Interestingly enough digging into the preset we are using, if you commented out Actually, maybe not: the I will continue investigating. Maybe |
Thank you for the reply and for providing a temporary solution. |
a mistake when install ▶ npm i @vue/[email protected]
npm ERR! code ETARGET
npm ERR! notarget No matching version found for @vue/[email protected].
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/shun/.npm/_logs/2020-07-29T06_19_36_580Z-debug.log somethine wrong when publish ? |
@lefreet If you are using the taobao registry mirror, please switch back to the official one. Seems its synchronization with the upstream registry is broken, I can't get the latest version synced back https://developer.aliyun.com/mirror/npm/package/@vue/cli-plugin-unit-jest |
OK, finally succeeded… Please try again. |
@sodatea all right, thanks. |
You can view documentation about |
Thanks, I should have read the docs 🤦 Anyway, I have found a fix. It looks like in Vue 2.x, the SFC tool (vue-template-compiler) would compile to commonjs. Vue 3.x compiler, The fix is to compile the template using babel. I was using TypeScript's I will ping @sodatea when I have fixed this and we can coordinate to include the latest vue-jest for the cli-plugin. |
Ok, fixed here: vuejs/vue-jest#260, I also explained how I tested it (basically reproduce this issue, change to my fixed vue-jest, ensure test passes). Will leave for a day or two so people can review if they want, then release a new version of vue-jest (5.x) this weekend. |
I released We should update the CLI plugin to use the latest |
@lmiller1990 Bad news: I did not have this issue previously (as I don't use Babel alongside TS), but I'm now encountering it with Repro: vue create vue-jest-alpha-2 --inlinePreset '{"useConfigFiles": true,"plugins": {"@vue/cli-plugin-typescript": {"classComponent": false},"@vue/cli-plugin-unit-jest": {}},"vueVersion": "3"}'
cd vue-jest-alpha-2
yarn test:unit throws: import { toDisplayString as _toDisplayString, createVNode as _createVNode, createTextVNode as _createTextVNode, createStaticVNode as _createStaticVNode, openBlock as _openBlock, createBlock as _createBlock } from "vue";
^^^^^^
SyntaxError: Cannot use import statement outside a module Open |
Ok so it's working with babel-jest but not with ts-jest now? VTU and vue-jest is definitely working in all my projects (non cli) so I guess we have another conflict in the cli-plugin somehow. Will investigate 👀 |
Problem is we are not compiling to cjs, but es modules (this import and export) and Jest doesn't know what to do, right? Should This can likely be fixed either here (jest config to make jest understand es modules) or in vue-jest (compile to cjs modules instead of es modules, since it seems |
Hmmm… It's very complicated. Previously the So it worked for TS without babel. After this change, only babel is used for compilation, because no babel config can be found in the project, the code remains unchanged. So the correct fix is not to use babel for all projects, but still use TS for compilation, only add babel to the pipeline if |
TS + Babel + all the various tooling is so complicated! I think I got it, though. I tested with:
All working. If you opt not to use babel, it won't work. I am not sure this workflow makes any sense, sfc-compiler spits out code using ES modules, which Jest cannot understand. @sodatea quick review? https://github.com/vuejs/vue-jest/pull/264/files @cexbrayat if you want to test it out, grab the latest vue-jest repo and do Can release once we are happy with it. I am sure there is a better way to write a jest transformer but better to get it working so people can try out Vue 3 and testing then optimize from there. |
@lmiller1990 I gave it a quick try with the repro I had, and your PR seems to work 🎉 |
Great, I will leave this for 24h-48h or so, so if anyone else has feedback (maybe Soda) they get a chance and release this weekend. |
I released |
@lmiller1990 I confirm that our builds succeed with alpha.3 (TS without babel), thank you 🎉 |
Now that it seems to have been fixed. I'm closing this issue now. |
Version
4.5.0
Reproduction link
https://github.com/adarean5/vue3-ts-jest
Environment info
Steps to reproduce
Run the test:unit script
What is expected?
The starter unit test (/tests/unit/example.spec.ts) should pass with output similar to:
What is actually happening?
Running test:unit produces the following:
This project was created with the following options selected:
I tried creating a new project with the same settings but with either Vue version 2 or JavaScript instead of TypeScript and they both seem to work.
I tried to apply fixes from what seems like a related issue (#1584) but sadly none of them worked.
The text was updated successfully, but these errors were encountered: