vue-cli: Default unit tests are failing
Version
3.0.0-rc.5
Reproduction link
Steps to reproduce
vue create vue-jest
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, Router, Vuex, Linter, Unit
? Pick a linter / formatter config: Basic
? Pick additional lint features: Lint on save
? Pick a unit testing solution: Jest
? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? In dedicated config files
? Save this as a preset for future projects? No
duplicate HelloWorld.spec.js
(i.e. Users.spec.js)
What is expected?
test should pass.
What is actually happening?
FAIL tests/unit/Users.spec.js
● Test suite failed to run
/lab/vue-jest/tests/unit/Users.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import "core-js/modules/es6.array.iterator";
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected string
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
In jest.config.js I added cache: false
to solve the error.
Other information:
- yarn v 1.6.0
- node v10.1.0
- macos High Sierra
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 25
- Comments: 48 (6 by maintainers)
Commits related to this issue
- Add logic for formatting phone numbers 휴대폰 번호를 '-' 없이 입력해도 포맷팅해서 DB에 저장할 수 있도록 필요한 함수 작성하고, jest로 유닛 테스트. jest 실행 시 transpile이 제대로 안 되는 문제가 있어서 process env 변수 추가로 해결. (Related issue: https://github.... — committed to greenmonn/chorongfarm-home by greenmonn 5 years ago
- fix: jestが落ちる対応(https://github.com/vuejs/vue-cli/issues/1879) ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import "core-js/modules/es6.array.iterator"; ... — committed to Thirosue/sample-vuejs-front by takeshi-hirosue 5 years ago
- fix: jestが落ちる対応(https://github.com/vuejs/vue-cli/issues/1879) ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import "core-js/modules/es6.array.iterator"; ... — committed to Thirosue/sample-vuejs-front by takeshi-hirosue 5 years ago
I believe I solved this problem by adding the following to the
jest.config.js
:I sadly have to say “I believe…”, because since I added this option to a newly created Vue project (using Vue CLI v3.0.0-rc.10) it suddenly also works for another project, where I didn’t add this option.
I am therefore not exactly sure what solved the issue and I am not able to reproduce it anymore. But I wanted to leave this here for others to try out.
The following links helped me to find this solution:
transformIgnorePatterns
option does and how to set it up for React Native.I also faced that issue. What helped me was:
babel-core
to~7.0.0-bridge.0
inpackage.json
npx jest --clearCache
After that everything worked again.
After spending some time debugging the issue, I think I’ve come up with a feasible solution. It seems like this comes down to a ‘wtf’ moment in terms of how NPM’s dependency resolution strategy works. The following seems to be the solution for me, but your mileage may vary depending on the specific scenario.
TLDR
You can do either of these things:
node_modules
and removepackage-lock.json
. Re-runnpm install
.npm cache verify
and re-runnpm install
.I personally think doing both is the best option to ensure everything is sound and your
package-lock.json
is more in-line with Vue CLI when it generates a fresh project.Long explanation
The intention seems to be that
cli-plugin-unit-jest
tries to depend on the root levelbabel-core
, which in combination withcli-plugin-babel
is usuallybabel-core@7.0.0-bridge.0
. This is normally fine, but for some unlucky people, NPM can incorrectly resolvebabel-core@6.26.3
as a nested dependency of the plugin (instead of being at the root level ofnode_modules
). As it’s essentially the wrong version, all sorts of weird things probably happen behind the scenes, but the bottom line is that Jest cannot transform the code correctly.Why does this happen? I’m not 100% sure (I am no expert in the NPM dependency resolution algorithm) but my best guess is that:
cli-plugin-unit-jest
relies onbabel-core@6.26.3
indirectly through one of it’s dependencies. I think it isbabel-jest
in this case, but as it is a peer dependency, it means thatbabel-core@7.0.0-bridge.0
should theoretically be used.When NPM has to resolve dependencies for
babel-jest
on the project level e.g. when updating it in my case, the algorithm gets confused and believesbabel-core
can’t be resolved as a peer dependency. I believe this is at least partly due to the NPM cache being corrupted or caching incorrect dependencies in some way. NPM resolvesbabel-core@6.26.3
as a nested dependency instead and everything breaks.It seems like the way to resolve this involves ensuring the NPM cache is correct and consistent (i.e.
npm cache verify
), and/or rebuilding thepackage-lock.json
andnode_modules
entirely to make sure it resolves everything from scratch again (without any external caching factors).@cars10 you just saved my life, if you accept bitcoin ill send you some 💸
@ChristianAEDev
Thanks, this solved it for me, until I change a test file. After running
./node_modules/jest/bin/jest.js --clearCache
the tests are working again.@cars10, also worked for me. After installing
babel-core
at~7.0.0-bridge.0
, we’re usingright now, to avoid the errors whenever we test. Obviously, that’s less than ideal if our tests start to get larger. And we’re also not super comfortable about still requiring
babel-core
when we should likely only be usingSo I’ll keep an eye on this
I found a workaround based on the one posted by @ebertti and the code of the vue-plugin-unit-test module.
Adding the following at the top of the jest.config.js file fixes the problem:
I tried every single suggestion provided so far, in vain.
@cars10 Solution worked for me.
This is not a perfect soluction, but I think this can be used temporarily
and creating a .env.test on project root dir with this values
It works great for me running jest directly on Jetbrains IDE test
Like the OP says, for me too putting
cache: false
in myjest.config.js
worked and solved the issueI Create a new project in vue-cli add “Unit Testing” and copy differences in my project. This work for me. Install the package:
change test:unit in
package.json
to:And create file
jest.config.js
with this value:In my test, work without
jest.config.js
.This worked like a charm! Thanks
This solution works fine. Please update the guide at https://vue-test-utils.vuejs.org/guides/testing-single-file-components-with-jest.html
It is not guiding to run the tests with jest through
vue-cli
I am getting a similar error as well, expect mine says
SyntaxError: Unexpected token import
This may be related to this issue:
https://github.com/vuejs/vue-cli/issues/1524#issue-330793755
babel.config.js
jest.config.js
Personally, I found this solution proposed by @Nivani to be sufficient without any other changes (in a project generated with CLI 3.0.5). I also found that if I don’t want to “pollute” the Jest config file, I can just set the env vars in the IDE’s run configuration.
Another option is to create a separate config file for the IDE to use that looks like this:
I called it
jetbrains-jest.config.js
and then selected it as the “Configuration file” in the Jest run configuration. That way, Vue CLI can be Vue CLI, and WebStorm can be WebStorm.The problem seems to be with the environment variables
VUE_CLI_BABEL_TARGET_NODE
andVUE_CLI_BABEL_TRANSPILE_MODULES
. If they are set to true, the issue goes away.Jianwu Chen on stackoverflow.com found this solution:
https://stackoverflow.com/questions/51365250/run-jest-got-unexpected-string-at-scripttransformer-transformandbuildscript
I used the vant component library when I was executing Vue-cli-service test:unit The following error was encountered during the command:
How should I solve the problem
Had the same issue. The root cause is if you use
npm
and it doesn’t hoist dependencies to thenode_modules
folder. Runnpm ls vue-jest babel-jest jest-transform-stub
, it should show that all deps at the root level:if not, then jest just cannot
require
transformers usingrequire
. Then you need to change the path to transformer. For example:this works in case you have this from
npm ls vue-jest
:Alternatively run
npm dedupe
to hoist and dedupe dependencies (it will update your package-lock.json as well, so the next time you won’t have that issue)I have solved my problem by switching to node LTS (10.16.3) and changing version of babel-jest to ^23.6.0. Now tests work as expected.
My problem was that the .npmrc pointed to website that hosted our repository’s configuration which was BEHIND A VPN and I happened to not be signed in when I originally ran
npm install
. Deleted and re-cloned the repo (prob could have just deleted the node_modules directory and package-lock.json) and made sure I was on the VPN when I rannpm install
. Worked like a charm (as it should have originally 🤦♂️ )Only change in transform section for ‘^.+\.jsx?$’ and ‘.*\.(vue)$’ and “babel-jest”: “^23.4.2” worked for me :
jest.config.js
So it’s just changing from ‘babel-jest’ to ‘<rootDir>/node_modules/babel-jest’ (and the same for vue-jest).
@killboard your solution was the one. In my case i followed this steps:
I’ve encountered same problem after updating
babel-jest
to version24.1.0
. None of the solutions helped. Getting back to23.6.0
solved problem for now.this is, IMHO, way faster than waiting for all tests to finish, when you’re only working on one file.
Though it should be noted that it can also be done using
npm run test:unit myComponent
. orvue-cli-service test:unit "myComponent"
(to run myComponent.spec.js) I’m sure it’s documented somewhere, but I haven’t come cross it and stopped looking. However you can’t (seem to) pass more than one argument, so for example, you may either clear a snaptshot or run a specific test, but you cannot clear a snapshot on a specific test.maybe this:
The simple
addition at the top of my jest config file seems to have fixed things for me as well.
@yyx990803, I have the same problem and no default
NODE_ENV
are set in my shell.For users seeing the same problem: do you have a default
NODE_ENV
set for your shell?Are you running
jest
directly? That’s not supposed to work. You must run it viavue-cli-service test:unit
.