Jest Vue Transformer (Legacy)
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
-
Cannot find module 'vue-template-compiler' from 'node_modules/vue-jest/lib/transformers/template.js'
cause `vue-template-compiler` is a required peer dependency but is not installed or its version is mismatched with `vue`.fix`npm install vue-template-compiler@<vue-version>` or `yarn add vue-template-compiler@<vue-version>`. Ensure the version matches your installed `vue` package. -
Jest encountered an unexpected token. This usually means that you are trying to import a file which Jest cannot process, e.g. a .vue file, without a proper transformer.
cause `vue-jest` is not correctly configured in `jest.config.js`'s `transform` section for `.vue` files, or `babel-jest` isn't configured for `.js` files in `node_modules`.fixCheck your `jest.config.js` `transform` entry for `vue-jest`: `{'^.+\.vue$': 'vue-jest'}`. Also, ensure `babel-jest` is configured for JavaScript files and `transformIgnorePatterns` allows transformation of relevant modules if needed. -
SyntaxError: Cannot use import statement outside a module
cause CommonJS vs. ES Module conflict. Vue SFCs often use ESM syntax, but Jest (depending on configuration and Node.js version) might be running in CommonJS mode. `babel-jest` or `ts-jest` need to correctly transpile ESM `import`/`export` statements.fixEnsure `babel-jest` (or `ts-jest` for TypeScript) is correctly configured in `jest.config.js` to transpile ES modules. For Babel, verify your `.babelrc` or `babel.config.js` includes `@babel/preset-env` with `modules: 'commonjs'` (or `auto` if Babel 7.x).
Warnings
- breaking The `vue-jest` package (v3.x and earlier) has been superseded. For Jest 28 and 29, and for distinct Vue 2 or Vue 3 projects, developers must migrate to `@vue/vue2-jest` or `@vue/vue3-jest` respectively. These new packages have different installation and configuration paths.
- gotcha `vue-jest@3.x` requires `vue-template-compiler` to be explicitly installed as a peer dependency, matching your `vue` version. Mismatched versions can lead to compilation errors or unexpected behavior.
- gotcha When using TypeScript in Vue SFCs, `vue-jest@3.x` typically relies on `babel-jest` or `ts-jest` for script block transformation. Incorrect configuration of these can lead to "unexpected token" errors or TypeScript compilation failures.
- deprecated The `vue-jest` package itself (versions 3.x and earlier) is considered deprecated by the Vue team in favor of the scoped `@vue/vue2-jest` and `@vue/vue3-jest` packages. While still functional, it receives minimal maintenance.
Install
-
npm install vue-jest -
yarn add vue-jest -
pnpm add vue-jest
Imports
- Transformer Configuration
import vueJest from 'vue-jest';
module.exports = { transform: { '^.+\.vue$': 'vue-jest' } };
Quickstart
/* 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);
});
});