Tutorials: Vue CLI tutorial

Wallaby.js runs your JavaScript tests immediately as you type and displays execution results in your code editor. Wallaby also provides beautiful test and code coverage reports updated in realtime.

In this tutorial we’re going to share how to configure Wallaby.js using the Vue.js CLI. We’ve structured this tutorial so you can follow along. If you don’t want to follow along then you may skip ahead and find the end result in this github repo.

Select your editor

The instructions in this tutorial are adjusted to your editor of choice. Please select your editor by clicking on your editor’s logo below before proceeding with the tutorial:

Install Dependencies

First, if you haven’t done so already, go ahead and download and install Wallaby.js.

Create a new Vue.js Application

Now that we’re ready to start, open your terminal and change directory to where we will create our new Vue.js application. Run the following command:

vue create wallaby-vuejs-app --default

Add Unit Tests to your project

At this point, your project doesn’t have any unit tests, so we’ll need to add them. In your terminal, change directory to your wallaby-vuejs-app and run the following command:

vue add @vue/unit-jest

Add Project Dependencies

Wallaby is designed to work with a number of frameworks and expects that your project has the wallaby-vue-compiler dependency that is necessary to run with Vue CLI projects.

The wallaby-vue-compiler package is a Wallaby.js compiler that parses and compiles .vue files and adds metadata that is necessary for Wallaby to correctly track code coverage information. You can explore the open source wallaby-vue-compiler package.

npm install wallaby-vue-compiler --save-dev

Configure Wallaby.js

If you are using Vue CLI 4+ that uses Jest >= v24, you can try our new feature that allows to use Wallaby without any configuration.

Next, open the new folder in your editor and create a new file in the project root, named wallaby.js. Copy the configuration shown below and paste it into the wallaby.js file. The configuration file below may seem foreign or scary but you don’t need to worry about what the configuration file is doing in order to use Wallaby.js with Vue (we have configured it for you).

module.exports = (wallaby) => {

  process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true;

  const compiler = wallaby.compilers.babel({presets: [['@vue/app', {modules: 'commonjs'}]]})

  return {
    files: ['src/**/*', 'jest.config.js', 'package.json'],

    tests: ['tests/**/*.spec.js'],

    env: {
      type: 'node'
    },

    compilers: {
      '**/*.js': compiler,
      '**/*.vue': require('wallaby-vue-compiler')(compiler)
    },

    preprocessors: {
      '**/*.vue': file => require('vue-jest').process(file.content, file.path)
    },

    setup: function (wallaby) {
      const jestConfig = require('./package').jest || require('./jest.config')
      jestConfig.transform = {}
      wallaby.testFramework.configure(jestConfig)
    },

    testFramework: 'jest',

    debug: true
  }
}

Start Wallaby.js

At this point, Wallaby.js is configured and is ready to be started. If this is the first time you are using wallaby, it may take a minute or two to install all the dependencies it needs.

In VS Code, launch the Command Pallete (Ctrl/Cmd + Shift + P) and then choose the Wallaby.js: Start command.

In Atom, launch the Command Pallete (Ctrl/Cmd + Shift + P) and then choose the Wallaby: Start command.

In Sublime Text, launch the Command Pallete (Ctrl/Cmd + Shift + P) and then choose the Wallaby.js: Start command.

In Visual Studio, right click on your Wallaby.js configuration file in your Solution Explorer, and then select Start Wallaby.js.

In your IntelliJ Editor, create a new wallaby.js Run Configuration. Select Edit configurations from the drop-down menu in the top right-hand corner of the navigation bar, or from the Run menu. Add a new Wallaby.js Configuration, specify the path to your wallaby.js configuration file and click OK. Once you have created and saved the configuration, you can start it. If you need help creating a run configuration, you may refer to JetBrains docs.

jetbrains run configuration

After Wallaby.js has started, navigate to your test file (tests/unit/example.spec.js), you will see coverage indicators in the gutter of your code editor:

  • Gray squares mean that the line contains code that was not run by any of your tests.
  • Green squares mean that the line contains code that was run by at least one of your tests.
  • Yellow squares mean that the line contains code that was only partially covered by your tests.
  • Red squares mean that the line is the source of an error, a failed assertion, or is part of a stack trace for an error.
  • Pink squares mean that the source line is on the execution path of a failing test.

It should look like this:

file markers in example.spec.js file markers in example.spec.js file markers in example.spec.js file markers in example.spec.js file markers in example.spec.js

If you open up src/components/HelloWorld.vue, you won’t see any coverage indicators. This is expected because there is currently no code that can execute.

Replace HelloWorld.vue with a simple counter

Let’s replace src/components/HelloWorld.vue to display a counter with an “increment” button:

<template>
  <div>
    <h1>{{ counter }}</h1>
    <button @click='increment()'>Increment</button>
  </div>
</template>

<script>
export default {
  data() {
    return { counter: 0 }
  },
  methods: {
    increment() { this.counter += 1; }
  }
}
</script>

Wallaby.js now shows line markers on HelloWorld.vue with coverage indicators in the gutter showing that tests are broken. The details of the broken tests are also listed in Wallaby’s test panel.

At this point, you might like to Toggle Wallaby’s Panel by launching the Command Pallete and selecting Wallaby.js: Toggle Tests View.

At this point, you might like to Toggle Wallaby’s Panel by launching the Command Pallete and selecting Wallaby: Toggle Panel.

broken tests broken tests broken tests broken tests broken tests

Fix our test

Now replace example.spec.js with the test below to test the logic for our new counter screen.

import { shallowMount } from '@vue/test-utils'
import HelloWorld from '@/components/HelloWorld.vue'

describe('HelloWorld.vue', () => {
  it('increments the counter when the button is clicked', () => {
    const wrapper = shallowMount(HelloWorld)

    for (let i = 0; i < 10; i++) {
      wrapper.find('button').trigger('click');
    }

    expect(wrapper.find('h1').text()).toBe("10");
  });
});

You will see that we now have we now have green coverage markers for our application code and our unit tests. The Wallaby.js test console also now reports that all tests are passing.

fixed tests fixed tests fixed tests fixed tests fixed tests

Learn More…

We’ve only covered the very basics of configuring and using Wallaby.js but the tool is capable of a whole lot more. Read through our docs to learn more about Wallaby.js features. Some notable features include:

  • Wallaby App updates in real-time and provides a strategic level view of coverage and test results.
  • Advanced Logging allows you to quickly and easily debug at runtime with keyboard shortcuts, comments, and console log.
  • Value Explorer provides the ability to explore runtime values right in your editor.

You may also learn about more Wallaby features in your editor specific tutorial your editor specific tutorial your editor specific tutorial your editor specific tutorial your editor specific tutorial.

Vue CLI 3.x - TypeScript and Jest

The configuration for Vue CLI 3.x TypeScript/Jest generated projects is provided below. Simply copy and paste the configuration into a new wallaby.js configuration file in your project root directory.

module.exports = wallaby => {
  process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true;

  return {
    files: ['src/**/*', 'jest.config.js', 'package.json', 'tsconfig.json'],

    tests: ['tests/**/*.spec.ts'],

    env: {
      type: 'node'
    },

    preprocessors: {
      '**/*.js?(x)': file => require('@babel/core').transform(
        file.content,
        {sourceMap: true, compact: false, filename: file.path, plugins: ['babel-plugin-jest-hoist']})
    },

    setup(wallaby) {
      const jestConfig = require('./package').jest || require('./jest.config')
      delete jestConfig.transform['^.+\\.tsx?$']
      wallaby.testFramework.configure(jestConfig)
    },

    testFramework: 'jest',

    debug: true
  }
}

Vue CLI 2.x - JavaScript and Jest (Webpack template)

Wallaby expects that your project has the wallaby-vue-compiler dependency that is necessary to run with Vue CLI projects.

yarn add wallaby-vue-compiler -D
   or
npm i wallaby-vue-compiler --save-dev

The configuration for Vue CLI 2.x TypeScript/Jest generated projects is provided below. Simply copy and paste the configuration into a new wallaby.js configuration file in your project root directory.

module.exports = function (wallaby) {
  return {
    files: ['src/**/*', 'test/unit/*.js', 'package.json'],

    tests: ['test/**/*.spec.js'],

    env: {
      type: 'node'
    },

    compilers: {
      '**/*.js': wallaby.compilers.babel(),
      '**/*.vue': require('wallaby-vue-compiler')(wallaby.compilers.babel({}))
    },

    preprocessors: {
      '**/*.vue': file => require('vue-jest').process(file.content, file.path)
    },

    setup: function (wallaby) {
      const jestConfig = require('./package').jest || require('./test/unit/jest.conf')
      jestConfig.transform = {};
      wallaby.testFramework.configure(jestConfig)
    },

    testFramework: 'jest',

    debug: true
  }
}

Troubleshooting

If you get stuck or something isn’t working for you, you may find a similar solved issue in our github issues repository. If you can’t find a solution, create a new issue.