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.

"SyntaxError: Unexpected token u in JSON at position 0" while using jest-vue

See original GitHub issue

Hi @eddyerburgh , I’m trying to use jest-vue with TypeScript support with Vue . My current setup of various files are as follows:

  1. package.json Jest Block
"jest": {
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
    "moduleFileExtensions": ["ts", "tsx", "vue", "js", "json", "jsx"],
    "verbose": true,
    "mapCoverage": true,
    "transform": {
      "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js",
      ".*\\.(vue)$": "<rootDir>/node_modules/jest-vue"
    }
  }

Also I’m using ts-jest, jest-vue in conjunction with "vue-typescript-import-dts": "^3.1.1" .

  1. tsconfig.json
{
  "compilerOptions": {
    "jsx": "preserve",
    "target": "es5",
    "lib": ["dom", "es2015"],
    "module": "es2015",
    "moduleResolution": "node",
    "outDir": "lib",
    "isolatedModules": false,
    "experimentalDecorators": true,
    "declaration": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": true,
    "removeComments": true,
    "suppressImplicitAnyIndexErrors": true,
    "allowSyntheticDefaultImports": true,
    "types": ["vue-typescript-import-dts"]
  },
  "include": ["./src/**/*"]
}
  1. Vue file components use lang="ts" and vue-class-component with vue-property-decorator packages

I’m getting the error as mentioned in Title, “SyntaxError: Unexpected token u in JSON at position 0” while using jest-vue

Not sure how to resolve this even after stumbling upon many links for solutions.

Full Stack Trace

> jest --coverage

 FAIL  __tests__/login.spec.ts
  ● Test suite failed to run

    SyntaxError: Unexpected token u in JSON at position 0
        at JSON.parse (<anonymous>)

      at compileTypescript (node_modules/jest-vue/lib/compilers/typescript-compiler.js:55:44)
      at processScript (node_modules/jest-vue/lib/process.js:14:12)
      at Object.module.exports [as process] (node_modules/jest-vue/lib/process.js:27:18)

  console.info node_modules/vue/dist/vue.runtime.common.js:7751
    You are running Vue in development mode.
    Make sure to turn on production mode when deploying for production.
    See more tips at https://vuejs.org/guide/deployment.html

Login Spec File

import Vue from 'vue'
import Login from '../src/components/login/Login'

describe('Hello Component', () => {
  let $mounted

  beforeEach(() => {
    $mounted = new Vue(Login).$mount()
  })

  test('snapshot', () => {
    let $html = $mounted.$el.outerHTML
    expect($html).toMatchSnapshot()
  })
})

Cannot share Login.vue file

Versions

    "jest": "^21.2.1",
    "jest-vue": "^0.8.1",
    "ts-jest": "^21.1.3",
    "ts-loader": "^3.0.5",
    "typescript": "^2.5.3",
    "vue-loader": "^13.3.0",
    "vue-template-compiler": "^2.5.2",
    "vue-typescript-import-dts": "^3.1.1",
    "vue": "^2.5.0",
    "vue-class-component": "^6.0.0",
    "vue-property-decorator": "^6.0.0",

Issue Analytics

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

github_iconTop GitHub Comments

80reactions
coldinocommented, Jun 12, 2018

This is the top Google result for this issue, so I’ll add a quick note. This is not related to jest-vue. For me the error was due to tsconfig.json not being 100% JSON-correct. Removing a few dangling commas sorted the problem.

0reactions
eddyerburghcommented, Oct 24, 2017

Glad you solved the issue. an example repo would be great 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Fix Unexpected token u in JSON at position 0 - Isotropic
From the causes, we can see that the fix for the Syntax Error "Unexpected token u in JSON at position 0" is relatively...
Read more >
Unexpected token u in JSON at position 0 - Stack Overflow
Try this in the console: JSON.parse(undefined). Here is what you will get: Uncaught SyntaxError: Unexpected token u in JSON at position 0 at ......
Read more >
Unexpected token u in JSON at position 0 - ItsJavaScript
The Unexpected token u in JSON at position 0 mainly occurs if we pass an undefined value to JSON.parse() method or $.parseJSON() method....
Read more >
jest typescript syntaxerror: unexpected token 'export' - You.com
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 >
Unexpected token u in JSON at position 0 - YouTube
JavaScript : Uncaught SyntaxError : Unexpected token u in JSON at position 0 [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] ...
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