Vue I18n Jest Transformer
vue-i18n-jest is a specialized Jest transformer designed to enable the correct parsing and transformation of `<i18n>` custom blocks within Vue Single File Components (SFCs) during unit testing. It specifically targets Vue 2 applications and integrates with `vue-jest@v4`. This package is crucial for developers who store their internationalization messages directly within their Vue components and need Jest to understand and process these blocks for testing. The current stable version is 1.1.1, released in October 2021. Its release cadence has been sporadic. It differentiates itself by providing a dedicated bridge for `vue-jest` to handle i18n custom blocks, which is not natively supported by `vue-jest` alone for Vue 2 testing environments.
Common errors
-
Jest encountered an unexpected token. This usually means that you are trying to import a file which Jest cannot parse, e.g. a .vue file with a custom block you have not configured a transformer for. (SyntaxError: Unexpected token <)
cause The `vue-i18n-jest` transformer is not correctly configured in `jest.config.js`, causing Jest to fail when parsing `<i18n>` blocks within Vue SFCs.fixVerify that `vue-i18n-jest` is installed and correctly added to your `jest.config.js` under `globals['vue-jest'].transform.i18n` as shown in the quickstart example. -
Error: Cannot find module 'vue-i18n-jest' from 'jest.config.js'
cause The `vue-i18n-jest` package is referenced in `jest.config.js` but is not installed as a development dependency.fixInstall the package using your preferred package manager: `npm install --save-dev vue-i18n-jest` or `yarn add -D vue-i18n-jest`. -
Minimum Node.js version of 12.0.0 is required. You are running 10.x.x.
cause Attempting to use `vue-i18n-jest` version 1.1.0 or newer with an unsupported Node.js runtime (version 10.x).fixUpgrade your Node.js environment to version 12 or a later compatible version. Consult your project's `.nvmrc` or CI/CD configuration.
Warnings
- breaking Starting with version 1.1.0, Node.js version 10 is no longer supported. Projects must upgrade to Node.js 12 or newer to use this version and subsequent releases.
- gotcha This transformer is specifically designed for `vue-jest@v4`, which is used for testing Vue 2 applications. It is not compatible with `vue-jest@v5` or testing environments for Vue 3.
- gotcha As of v1.1.1, `vue-jest` was removed from `vue-i18n-jest`'s peerDependencies. While no longer explicitly listed, `vue-jest@v4` remains a functional dependency for this package to operate correctly.
- gotcha `vue` and `vue-template-compiler` are essential peer dependencies for any Vue 2 testing setup using `vue-jest`. Failure to install compatible versions will lead to Jest errors.
Install
-
npm install vue-i18n-jest -
yarn add vue-i18n-jest -
pnpm add vue-i18n-jest
Imports
- Jest Configuration (Vue SFC i18n blocks)
// jest.config.js module.exports = { transform: { '^.+\.vue$': 'vue-i18n-jest' // Incorrectly trying to use it as the primary Vue transformer } };// jest.config.js module.exports = { // ... other Jest configurations globals: { 'vue-jest': { transform: { 'i18n': 'vue-i18n-jest' // This line enables the transformer for i18n blocks } } } }; - Direct Transformer Reference (Less Common)
import { process } from 'vue-i18n-jest'; // This package exports a default function, not named exports for direct usage// jest.config.js module.exports = { transform: { '^.+\.(i18n|json5)$': 'vue-i18n-jest' // Example: directly mapping i18n custom blocks if extracted to separate files } }; - CommonJS/ESM Module Import (Discouraged)
const i18nTransformer = require('vue-i18n-jest'); // or import i18nTransformer from 'vue-i18n-jest';
Quickstart
// jest.config.js
const path = require('path');
module.exports = {
// Specify test environment (e.g., jsdom for browser-like DOM environment)
testEnvironment: 'jsdom',
// List of file extensions Jest should look for
moduleFileExtensions: ['js', 'json', 'vue', 'ts'],
// How to transform files based on their extension
transform: {
'^.+\.vue$': 'vue-jest', // Use vue-jest for Vue SFCs
'^.+\.js$': 'babel-jest', // Use babel-jest for JavaScript files
'^.+\.ts$': 'ts-jest' // Use ts-jest for TypeScript files (if applicable)
},
// Global settings for specific transformers, especially for vue-jest
globals: {
'vue-jest': {
// Enable vue-i18n-jest to process <i18n> blocks within Vue SFCs
transform: {
'i18n': 'vue-i18n-jest'
}
}
},
// Set up module name mapping for imports, e.g., '@/components' for 'src/components'
moduleNameMapper: {
'^@/(.*)$': path.resolve(__dirname, 'src/$1')
},
// Snapshot serializer for Vue components (recommended for better readability)
snapshotSerializers: [
'jest-serializer-vue'
]
// Ensure vue and vue-template-compiler are installed as devDependencies.
// This setup assumes a basic Vue 2 project with TypeScript (optional) and Babel.
};