How to solve Jest error with Vue
With vue-cli RC9
it was not straight forward to test a ES6 file. I found some advices but none worked for me. Here is the error I had:
Jest encountered an unexpected token
// ...
SyntaxError: Unexpected token {
// ...
This was happening on line 1 of the first test file… So it seems the ES6 import
was not understood and the my code was not being transpiled by babel
The file jest.config.js
was missing an instruction to to transform js
files as well. So I added it:
transform: {
“^.+\\.(js|jsx)?$”: “babel-jest”,
But still no luck, same error, I cleaned the cache, reinstalled all the node_modules, nothing was working :(
Finally the problem is that jest
was unable to find the module babel-jest
… I just had to do:
transform: {
“^.+\\.(js|jsx)?$”: “<rootDir>/node_modules/babel-jest”,
and it all started to work.
I hope this helps someone.