I realize this question has been asked several times but all of the solutions I've come across don't seem to work for me. I'm running into the following error while trying to run Jest tests for a Vue app.
Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript. By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules". Here's what you can do: • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config. • If you need a custom transformation specify a "transform" option in your config. • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option. You'll find more details and examples of these config options in the docs: https://facebook.github.io/jest/docs/en/configuration.html Details: /node_modules/vue-awesome/icons/expand.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Icon from '../components/Icon.vue' ^^^^^^ SyntaxError: Unexpected token import > 17 | import 'vue-awesome/icons/expand'
.babelrc:
{ "presets": [ ["env", { "modules": false, "targets": { "browsers": ["> 1%", "last 2 versions", "not ie <= 8"] } }] ], "env": { "test": { "presets": [ ["env", { "targets": { "node": "current" }}] ] } } }
jest config in package.json:
"jest": { "moduleFileExtensions": [ "js", "vue" ], "moduleNameMapper": { "^@/(.*)$": "<rootDir>/src/$1" }, "transform": { "^.+\\.js$": "<rootDir>/node_modules/babel-jest", ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest" }, "snapshotSerializers": [ "<rootDir>/node_modules/jest-serializer-vue" ], "moduleDirectories": [ "node_modules", "src" ] }
It looks like the initial import in the script for the Vue component being mounted for the test is working but the import within the module itself (import Icon from '../components/Icon.vue
) is not recognized.
boiler plate repo to re-creates the issue: github.com/DonaldPeat/stackoverflow-jest-question
How can I resolve this?
Jest is an open-source Javascript testing framework developed by Facebook. It was mainly built for React, Node, Babel, TypeScript, Angular, Vue, and JavaScript-based applications.
To read TypeScript configuration files Jest requires ts-node . Make sure it is installed in your project.
I used transformIgnorePatterns right below the preset of jest in the package. json file. transformIgnorePatterns option can be used to specify which files shall be transformed by Babel.
You just need to make sure that vue-awesome
will be transformed by jest, so add following to your jest config:
transformIgnorePatterns: ["/node_modules/(?!vue-awesome)"],
which means: "Ignore everything in node_modules except for vue-awesome
.
Also here is exhausive list of other issues that might cause this error: https://github.com/facebook/jest/issues/2081
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With