Vue Test Utils Compatibility Layer

0.0.15 · active · verified Tue Apr 21

vue-test-utils-compat provides a critical compatibility layer for migrating existing Vue 2 test suites written with `@vue/test-utils` v1 to run seamlessly on `@vue/test-utils` v2 and Vue 3. This package, currently at version 0.0.15, is designed to ease the transition burden by allowing developers to upgrade their Vue components to Vue 3 first, and then address test migrations, leveraging Vue 3's migration build. It achieves this by providing numerous compatibility flags that can be configured to mimic v1 behavior, addressing differences in mounting options, wrapper APIs, and event handling. Its release cadence is tied to the needs of the Vue 2 to Vue 3 migration process. A key differentiator is its extensive configuration options, allowing granular control over which v1 behaviors are re-enabled, facilitating a gradual and less disruptive migration path.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to install the compatibility layer for `@vue/test-utils` v2 in a Vue 3 project and provides a basic example of testing a component with v1-style APIs.

import { h } from 'vue';
import * as VueTestUtils from '@vue/test-utils';
import { installCompat, fullCompatConfig } from 'vue-test-utils-compat';

// Assuming Vue 3 without migration build
// If using 'vue/compat', the 'h' function might need to be retrieved from a mounted app instance.

installCompat(VueTestUtils, fullCompatConfig, h);

// Example test using the compat layer
describe('MyComponent with VTU v1 compat', () => {
  const MyComponent = {
    template: '<div><p>{{ message }}</p><button @click="increment">Click me</button></div>',
    data: () => ({ message: 'Hello', count: 0 }),
    methods: { increment() { this.count++; } }
  };

  it('renders the message', () => {
    const wrapper = VueTestUtils.mount(MyComponent);
    expect(wrapper.find('p').text()).toBe('Hello');
  });

  it('increments count on button click (v1 style)', async () => {
    const wrapper = VueTestUtils.mount(MyComponent);
    await wrapper.find('button').trigger('click');
    // In v1, wrapper.vm would directly expose the component instance
    // With compat, this attempts to bridge that gap.
    expect(wrapper.vm.count).toBe(1);
  });
});

view raw JSON →