question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

vue + jest + typescript: Unexpected token import

See original GitHub issue

Hi all, I have a Typescript file which imports a Vue file. It seems vue-jest doesn’t like this?

/path-to-my-project/Autocomplete/Autocomplete.vue:14
import ItemTemplate from './_autocomplete_item.vue';
^^^^^^

SyntaxError: Unexpected token import

> 1 | import Autocomplete from './Autocomplete/Autocomplete.vue';
    | ^

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
  at Object.<anonymous> (path-to-my-project/base.ts:1:1)

You’ll note that it’s able to find Autocomplete.vue, but is unable to parse the import within Autocomplete.vue.

Here is my jest config; Am I doing something obviously wrong here?

  "jest": {
    "transform": {
      "^.+\\.tsx?$": "ts-jest",
      "^.+\\.vue$": "vue-jest"
    },
    "testRegex": "(\\./test/javascript/.*|\\.(test|spec))\\.(jsx?|tsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "vue",
      "js",
      "jsx",
      "json",
      "node"
    ]
  }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

23reactions
francoismassartcommented, Feb 8, 2019

I finally found a way to make it work 🎉🎉🎉 by adding these lines to the jet.config.js:

  preset: 'ts-jest/presets/js-with-ts',
  transformIgnorePatterns: ['node_modules/(?!(bootstrap-vue)/)'],

Thanks to: https://stackoverflow.com/questions/53397773/node-modules-not-being-parsed-correctly-for-tests/53414070#53414070

I also had to add "allowJs": true, to my tsconfig.json

I hope it can help you out!

17reactions
francoismassartcommented, Feb 7, 2019

Any news on this issue ? I also get this error 👎

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest encountered an unexpected token when testing a Vue ...
This usually means that you are trying to import a file which Jest cannot parse, e. ... g. it's not plain JavaScript. By...
Read more >
Test Vue with Jest fail, Jest encountered an unexpected token ...
Coding example for the question Test Vue with Jest fail, Jest encountered an unexpected token, SyntaxError: Unexpected token import-Vue.js.
Read more >
How I Fixed The Unexpected Token Error In Jest
Depending upon your setup, you might see the error on the first line of the code file or you might see it when...
Read more >
test suite failed to run jest encountered an unexpected token
This means that a file is not transformed through TypeScript compiler, e.g. because it is a JS file with TS syntax, or it...
Read more >
Jest throwing `SyntaxError: Unexpected token import` on new ...
Jest throwing `SyntaxError: Unexpected token import` on new Vue CLI ... Not sure what in means, seems like some internal problem. Anyone seen...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found