vue-test-helpers
Helpers and syntactic sugars on top of vue-test-utils.
Install
First install the package with yarn or npm:
$ yarn add vue-test-helpers --dev
Also you'll need a peer dependency of vue-test-utils
. Install it with:
$ yarn add @vue/test-utils --dev
Then, before your tests (for example in a setup script), import and call the function:
This will do two things:
- By default, make
mount
andshallowMount
(also aliased asshallow
) available globally, so you don't need to manuallyimport { mount, shallowMount } from '@vue/test-utils'
at the beginning of every test file. If this behavior is not what you want, just callsetupHelpers({ registerGlobals: false })
instead. - Add some helpers and syntactic sugars on top of
Wrapper
to create your test a better experience. See the next section for details.
Available helpers
These helpers are available on
Wrapper
instances only, since I'm not a fan ofWrapperArray
which is just a very thin wrapper around an array ofWrapper
's. If you are dealing with aWrapperArray
, just iterate through its.wrappers
collection and run the helpers on each item.
.has(selector)
: alias for.contains(selector)
.hasAll|containsAll(...selectors)
: asserts that the wrapper has all provided selectors
.hasAny|containsAny(...selectors)
: asserts that the wrapper has any of the provided selectors
.hasNone|containsNone(...selectors)
: asserts that the wrapper has none of the provided selectors
.hasClass|hasClasses(...classes)
: asserts that the wrapper has the CSS class(es).
.hasAttribute(name, value)
: asserts that the wrapper has an attributename
with the valuevalue
.hasProp(name, value)
: asserts that the wrapper has a propname
with the valuevalue
Note:
hasClass
,hasAttribute
, andhasProp
are actually available invue-test-utils
, but marked as deprecated and will be removed in 1.0.
.hasEmitted(name[, value])
: asserts that an eventname
has been emitted, optionally with a valuevalue
wrappervm
.id()
: gets the id of the contained element
.click|dblclick|input|submit|focus|blur|change([options])
: triggers the click/dblclick/input/submit/focus/blur/change event on the contained element, optionally with anoptions
object. These methods return the wrapper instance, useful for chaining.
.click|dblclick|input|submit|focus|blur|change(selector[, options])
: finds the contained element byselector
and triggers the click/dblclick/input/submit/focus/blur/change event on it, optionally with anoptions
object. These methods return the wrapper instance, useful for chaining.
.setValue(value)
: sets the value of the contained (input) element. This method returns the called instance, useful for chaining.
wrapper
.getValue()
: gets the value of the contained (input) element
.value
: a proxy for the value of the contained (input) element
wrappervalue = 'foo'
License
MIT © Phan An