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.

Vuejs + Jest : “SyntaxError: Unexpected token <”

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
LinusBorgcommented, Jul 2, 2019

Judging from the error message referring to a file in a node_modules package:

path/node_modules/vue-loading-spinner/src/components/Jumper.vue:1

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:

{
  "transformIgnorePatterns": ["/node_modules/(?!vue-loading-spinner)"]
}
0reactions
manuel-84commented, Dec 21, 2021

Well, your case doesn’t seem special, it should work with a matching transformIgnorePatternike described in m previous comment.

I don’t see anything else to do or try.what pattern did you try?

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

Read more comments on GitHub >

github_iconTop 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 >

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