Vuejs + Jest : “SyntaxError: Unexpected token <”
See original GitHub issueI have a Vue file which imports a Vue file from node_modules. This file seems to not be recognize as a vue file when imported and doesn’t use vue-jest.
I tried to use an htmlLoader, but doesn’t work either.
Here is the error I get :
path/node_modules/vue-loading-spinner/src/components/Jumper.vue:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){<template>
SyntaxError: Unexpected token <
Here is my jest.conf.js :
const path = require('path')
module.exports = {
rootDir: path.resolve(__dirname, '../../'),
moduleFileExtensions: [
'js',
'vue',
'html'
],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
transform: {
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',
'.*\\.vue$': '<rootDir>/node_modules/vue-jest',
"^.+\\.html$": "<rootDir>/test/utils/htmlLoader.js"
},
testPathIgnorePatterns: [
"<rootDir>/node_modules/"
],
modulePaths: [
"<rootDir>"
],
setupFiles: ['<rootDir>/test/unit/setup'],
testMatch: [
'<rootDir>/(test/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx))'
]
}
And here is my test file ( I know there is no test in this file, but not necessary at the moment if I can mount my component ) :
import { shallowMount } from "@vue/test-utils"
import Card from '@/components/Card'
import { propsSet } from '../../datas/components/Card'
import Brace from 'vue-bulma-brace'
describe('FreeSql Unit Tests', () => {
test('FreeSql : Display correct', () => {
const wrapper = shallowMount(FreeSql, {
propsData:{
...propsSet
},
stubs:{
Brace
}
})
})
})
Thanks for your help.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Jest encountered an unexpected token when testing a Vue ...
Jest + Vue - SyntaxError: Unexpected token < ... of the vue-jest to what is shown in the documentation. https://github.com/vuejs/vue-jest.
Read more >Jest error unexpected token - Get Help - Vue Forum
but I have a problem when try to do actions.js tests with jest ... VueSingleSelect.vue'; ^^^^^^ SyntaxError: Unexpected token import 14 ...
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 >Vue Data table and jest error? unexpected Token - JavaScript
... Details: SyntaxError: Unexpected token (1:251) at Parser.pp$4.raise (node_modules/vue-template-es2015-compiler/buble.js:2757:13) at ...
Read more >jest syntaxerror: unexpected token '.' - You.com | The Search ...
reactjs - Jest SyntaxError: Unexpected token < - Stack Overflow ... Jest encountered an unexpected token This usually means that you are trying...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Judging from the error message referring to a file in a node_modules package:
We can see that your .vue file itself is recognized fine, but jest seems to not transform stuff from node_modules.
And that’s indeed the default behaviour:
https://jestjs.io/docs/en/23.x/configuration#transformignorepatterns-array-string
Solution
Excempt this package from the
transformIgnorePatterns
option with a negatiuve lookahead:for transform i tried
^.+\\.vue$
and^(.*).vue$
and some variations for transformIgnorePattern/node_modules/(?!(v-snackbars)/)
and little variations (and also without it at all)I don’t know if it matters that i’m on windows machine I’m also running the tests with “–no-cache” option for this case