1

Basically, the component isn't getting compiled, so I get an Unexpected token < error when it runs into <template>

I've run the following commands:

$ npm install --save-dev jest
$ npm install --save-dev vue-jest
$ npm install --save-dev vue-test-utils

and I've out the following in package.json:

"scripts": {
    "dev": "node build/dev-server.js",
    "build": "node build/build.js",
    "test": "jest"
  },

...

"jest": {
    "unmockedModulePathPatterns": [
        "<rootDir>/node_modules/vue"
    ],
    "moduleFileExtensions": [
        "js",
        "vue"
    ],
    "scriptPreprocessor": "index.js"
}

I created a __test__ folder in the root directory with a simple test:

const Vue = require("vue");
const VueTestUtils = require("vue-test-utils");

Vue.config.debug = true;
Vue.config.async = false;

Vue.use(VueTestUtils.install);

import Hello from '../src/components/Hello.vue'

const Constructor = Vue.extend(Hello)
const vm = new Constructor().$mount()

describe('initial test', () => {
  it('should be 1', () => {
    expect(1).toBe(1)
  })
})

I recently got this error as well, and not quite sure how to configure Vue.js so it will run using the compiler-included build:

[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

Been looking around for a while, so any help would be appreciated

3
  • 1
    You should check this page, it explains the differences and how to setup runtime and full versions vuejs.org/v2/guide/installation.html Commented Apr 1, 2017 at 8:24
  • Thanks, I read a bunch of the guide but completely skipped this after installing by npm install vue vue-cli Commented Apr 2, 2017 at 1:13
  • Still no luck though after configuring the webpack build files, i'll update if I make progress Commented Apr 2, 2017 at 1:13

1 Answer 1

2

You need to use a Jest transform to transform Jest Vue files. A Jest transformer is a synchronous function, that takes the file and path as input and outputs transpiled code.

I maintain a npm package that does it for you - vue-jest. npm install --save-dev vue-jest

You need to add a jest section to your package.json (or in a seperate file with --config). Your config should look something like this:

  "jest": {
  "moduleFileExtensions": [
    "js",
    "json",
    "vue"
  ],
  "transform": {
    "^.+\\.js$": "babel-jest",
    ".*\\.(vue)$": "vue-jest"
  }
}

This tells jest to use jest-vue as a transform for files with a .vue extension.

You can see a working repo using Vue Test Utils here - https://github.com/eddyerburgh/vue-test-utils-jest-example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.