Skip to content

Latest commit

 

History

History
138 lines (90 loc) · 3.49 KB

config.md

File metadata and controls

138 lines (90 loc) · 3.49 KB

Config

Vue Test Utils includes a config object to defined options used by Vue Test Utils.

Vue Test Utils Config Options

stubs

  • type: { [name: string]: Component | boolean | string }
  • default: { transition: TransitionStub, 'transition-group': TransitionGroupStub }

The stub stored in config.stubs is used by default.
Stubs to use in components. These are overwritten by stubs passed in the mounting options.

When passing stubs as an array in the mounting options, config.stubs are converted to an array, and will stub components with a basic component that returns <${component name}-stub>.

Example:

import { config } from '@vue/test-utils'

config.stubs['my-component'] = '<div />'

mocks

  • type: Object
  • default: {}

Like stubs, the values passed to config.mocks are used by default. Any values passed to the mounting options mocks object will take priority over the ones declared in config.mocks.

Example:

import { config } from '@vue/test-utils'

config.mocks['$store'] = {
  state: {
    id: 1
  }
}

methods

  • type: { [name: string]: Function }
  • default: {}

You can configure default methods using the config object. This can be useful for plugins that inject methods to components, like VeeValidate. You can override methods set in config by passing methods in the mounting options.

Example:

import { config } from '@vue/test-utils'

config.methods['getData'] = () => {}

provide

  • type: Object
  • default: {}

Like stubs or mocks, the values passed to config.provide are used by default. Any values passed to the mounting options provide object will take priority over the ones declared in config.provide. Please take note that it is not supported to pass a function as config.provide.

Example:

import { config } from '@vue/test-utils'

config.provide['$logger'] = {
  log: (...args) => {
    console.log(...args)
  }
}

silent

  • type: Boolean
  • default: true

It suppresses warnings triggered by Vue while mutating component's observables (e.g. props). When set to false, all warnings are visible in the console. This is a configurable way which relies on Vue.config.silent.

Example:

import { config } from '@vue/test-utils'

config.silent = false

autoDestroy

  • type: boolean | Function
  • default: false

This allows you to call wrapper.destroy() automatically—either when creating a new wrapper or by passing a hook function.

Please note that setting autoDestroy: true only destroys existing instances when a new instance is created which result in one wrapper instance remaining. Therefore passing a hook function is usually a better idea.

Examples:

import { config, mount } from '@vue/test-utils'

config.autoDestroy = true

const Component = {
  template: '<div>come ponente</div>'
}

const firstWrapper = mount(Component)
expect(firstWrapper.text()).not.toBe('come levant')

const secondWrapper = mount(Component) // this will call firstWrapper.destroy()
expect(firstWrapper.text()).not.toBe('come sirocco')
import { config, mount } from '@vue/test-utils'

config.autoDestroy = afterEach // will call wrapper.destroy() after each test case

const Component = {
  template: '<div>come ponente</div>'
}

describe('my component', () => {
  it('is not East wind', () => {
    const wrapper = mount(Component)
    expect(wrapper.text()).not.toBe('levant')

    // wrapper.destroy() is called after this
  })
})