Jest Vue Transformer (Legacy)

3.0.7 · maintenance · verified Sun Apr 19

vue-jest is a Jest transformer designed to enable testing of Vue Single File Components (SFCs) within a Jest environment. Version 3.0.7 is part of the legacy branch primarily supporting Vue 2.x applications and compatible with Jest versions up to Jest 27. This package transpiles Vue SFCs, including script, template, and style blocks, into a format Jest can understand. While still functional for older projects, it has been largely superseded by the `@vue/vue2-jest` and `@vue/vue3-jest` packages, which offer dedicated support for Vue 2 and Vue 3 respectively, alongside Jest 28 and 29. Developers working on new projects or upgrading Jest should use the scoped `@vue` packages. The release cadence for `vue-jest@3.x` is now minimal, focused mainly on critical fixes for existing users, as active development has shifted to the scoped packages. Its key differentiator was being the official Vue team's solution for Jest testing before the package split.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure `vue-jest` in `jest.config.js` for testing a basic Vue 2 component, including an example component and its corresponding unit test using `@vue/test-utils`.

/* jest.config.js */
module.exports = {
  moduleFileExtensions: ['js', 'json', 'vue'],
  transform: {
    '^.+\.vue$': 'vue-jest',
    '^.+\.js$': 'babel-jest',
    // If using TypeScript, you might need 'ts-jest'
    // '^.+\.ts$': 'ts-jest',
  },
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
  },
  testEnvironment: 'jsdom',
  snapshotSerializers: ['jest-serializer-vue'],
};

/* src/components/HelloWorld.vue */
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  }
};
</script>

<style scoped>
h3 {
  margin: 40px 0 0;
}
</style>

/* tests/unit/HelloWorld.spec.js */
import { shallowMount } from '@vue/test-utils';
import HelloWorld from '@/components/HelloWorld.vue';

describe('HelloWorld.vue', () => {
  it('renders props.msg when passed', () => {
    const msg = 'new message';
    const wrapper = shallowMount(HelloWorld, {
      propsData: { msg }
    });
    expect(wrapper.text()).toMatch(msg);
  });
});

view raw JSON →