Vue I18n Jest Transformer

1.1.1 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This configuration snippet for `jest.config.js` demonstrates how to integrate `vue-i18n-jest` into your Jest setup to correctly parse `<i18n>` custom blocks within Vue 2 Single File Components when using `vue-jest`.

// 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.
};

view raw JSON →