Socket
Socket
Sign inDemoInstall

@vue/test-utils

Package Overview
Dependencies
0
Maintainers
4
Versions
119
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @vue/test-utils

The next iteration of Vue Test Utils. It targets Vue 3.


Version published
Maintainers
4
Created

Package description

What is @vue/test-utils?

@vue/test-utils is the official unit testing utility library for Vue.js. It provides methods to mount and interact with Vue components in an isolated manner, allowing developers to write unit tests for their Vue components effectively.

What are @vue/test-utils's main functionalities?

Mounting Components

Mounting components allows you to render a Vue component in a test environment and returns a wrapper that contains the mounted component and methods to interact with it.

import { mount } from '@vue/test-utils';
import MyComponent from './MyComponent.vue';

const wrapper = mount(MyComponent);

Finding Elements

Finding elements within the mounted component using CSS selectors, Vue component options, or ref attributes, which can then be used to test the component's behavior.

const button = wrapper.find('button');

Simulating User Interaction

Simulating user interactions such as clicks, typing, or other events on the mounted component to test how it responds to user input.

button.trigger('click');

Accessing Component Data and Methods

Accessing the Vue instance's data, computed properties, and methods to assert their values and behavior within the test.

const vm = wrapper.vm;
console.log(vm.someData);

Stubbing Child Components

Stubbing child components to prevent their actual implementation from being rendered, which can simplify tests and improve test performance.

const wrapper = mount(MyComponent, {
  stubs: {
    ChildComponent: true
  }
});

Other packages similar to @vue/test-utils

Readme

Source

Vue Test Utils (next)

The next iteration of Vue Test Utils. It targets Vue 3.

Installation and Usage

  • yarn: yarn add @vue/test-utils@next --dev
  • npm: npm install @vue/test-utils@next --save-dev

Get started with the documentation.

Coming from Vue 2 + Vue Test Utils?

Check the migration guide. It's still a work in progress. If you find a problem or something that doesn't work that previously did in Vue Test Utils v1, please open an issue.

Documentation

See the docs.

Development

Get started by running yarn install. You can run the tests with yarn test. That's it!

Comparison with Vue Test Utils v1 (targeting Vue 2)

This is table for those coming from VTU 1, comparing the two APIs. Some things are still a work in progress.

  • ✅ - implemented
  • ❌ - not yet implemented
  • ⚰️ - will not be implemented (if you have a compelling use case, please open an issue)

Mounting Options

optionstatusnotes
data
slots
mocksnested in global
propsDatanow called props
providenested in global
mixins(new!) nested in global
plugins(new!) nested in global
component(new!) nested in global
directives(new!) nested in global
stubs
attachToDocumentrenamed attachTo. See here
attrs
scopedSlots⚰️scopedSlots are merged with slots in Vue 3
context⚰️different from Vue 2, does not make sense anymore.
localVue⚰️no longer required - Vue 3 there is no global Vue instance to mutate.
listeners⚰️no longer exists in Vue 3
parentComponent⚰️

Wrapper API (mount)

methodstatusnotes
attributes
classes
exists
findonly querySelector syntax is supported. find(Comp) under discussion here
emitted
findAllsee above. .vm is different to Vue 2. We are exploring options.
get
html
setValueworks for select, checkbox, radio button, input, textarea. Returns nextTick.
text
triggerreturns nextTick. You can do await wrapper.find('button').trigger('click')
setProps
props
setData
destroyrenamed to unmount to match Vue 3 lifecycle hook name.
props
isVisible
contains⚰️use find
emittedByOrder⚰️use emitted
setSelected⚰️now part of setValue
setChecked⚰️now part of setValue
is⚰️
isEmpty⚰️use matchers such as this
isVueInstance⚰️
name⚰️
setMethods⚰️

FAQs

Last updated on 24 Sep 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc