VueUnit
A library for Vue.js that makes it easier to create and unit test components.
- Easily test props, events, and slots (including named slots)
- Optionally shallow render components
- Simulate simple DOM events (click, input, etc.)
- Use any test runner / assertion library
- Mock and stub vuex actions and getters
Unlike other component testing libraries, VueUnit does not focus on DOM traversal. Instead it focuses on making programmatic manipulation of your components much easier. For DOM traversal and assertions it's strongly recommended to use a library such as chai-jquery
or jasmine-jquery
.
Installation
npm install --save-dev vue-unit
VueUnit requires "vue": "2.*"
to be installed, using the standalone build.
Usage
Examples shown are using mocha, chai, sinon, and chai-jquery but you should be able to extrapolate to your framework of choice.
Hooks
VueUnit renders all components in the DOM and needs to handle setup and tear down. VueUnit provides two helpers for this: beforeEachHooks()
and afterEachHooks()
when need to be called before and after each test, respectively.
For example with mocha this would look like:
You should assume all example tests shown are using these hooks.
mount()
The mount()
function renders components and allows setting props, listening for events, or putting content into slots.
Basic Component
const BasicComponent = template: `<div class="component">Hello</div>`
The first argument mount()
expects is a Vue component.
Component with props
const ComponentWithProps = template: ` <div class="component"> Hello {{ message }} </div> ` props: 'message'
An object representing the data for props, can be passed the second argument.
Component with events
const ComponentWithEvent = template: ` <button @click="$emit('foo', 'bar')">Click</button> `
The 3rd argument mount()
accepts is an object with listener functions for events to emit. Here we are use the simulate()
function to fire a click event.
Component with default slot
const ComponentWithSlot = template: ` <div class="component"> <slot></slot> </div> `
Component with multiple slots
const ComponentWithMultipleSlots = template: ` <div class="component"> <slot></slot> <slot name="foo"></slot> </div> `
The 4th argument mount()
accepts is an object with values available to be injected via the provide/inject behaviour. It is equivalent to defining provide
on an ancestor component.
Component with injects
const ComponentWithInjects = template: ` <div class="component"> Hello {{ message }} </div> ` inject: 'message'
Options Object
const ComponentWithAllOptions = template: ` <div class="component" @click="$emit('foo', message)"> <slot></slot> <slot name="foo"></slot> </div> ` props: 'message' inject: 'messageFromAncestor'
Option | Default | Type | Description |
---|---|---|---|
props |
{} |
Object |
An object containing camelCased props to be passed to the component |
on |
{} |
Object |
An object containing listeners to be called when an event is trigger by the component |
slots |
{} |
Object or String |
String containing the template for the default slot of the component. Objects containing templates for slots (should be keyed by the slot name). |
provide |
{} |
Object |
An object simulating values given from parent components via Vue's provide . Available to the equivalent inject component property. |
shallow()
The shallow()
function has the same signature as the mount()
function, however it will shallow render the component by replacing any locally registered child components with an empty kebab-cased tag of their name.
This enables you to test higher-level components without the concern of the required props or dependencies of nested child components.
For example:
const ChildComponent = name: 'child' template: '<span>World</span>'const ComponentWithChild = template: '<div>Hello <child></child></div>' components: ChildComponent
Would render:
Hello
Instead of the actual child component.
build()
and buildShallow()
When testing components using mount()
or shallow()
it can be repetitive to list the component and select DOM elements. The build()
and buildShallow()
functions allow you to remove some of this repetitive by creating your own mount()
or shallow()
function with some default arguments and a callback.
For example given this component:
const SomeComponent = template: '<p>{{ message }}</p>' props: foo: default: 'default message'
and this test:
We could use build()
like so:
let paragraphconst mount =
This can be helpful when your components has many selectors you want to test.
simulate(element, event)
The simulate()
function will trigger an HTML event on given DOM element. It can accept an DOM element or jQuery object.
For example, triggering a click event on a button would look like so:
const button1 = const button2 = document
waitForUpdate(cb)
The waitForUpdate()
helper function can be used to clean up a chain of async DOM assertions that you would normally have to use Vue.nextTick()
for.
For example we could rewrite the following test:
const Component = template: `<p>{{ message }}</p>` props: 'message'
like so:
Please note when using waitForUpdate()
use must end your chain of assertions with .end(done)
to avoid errors.
Vuex Testing
VueUnit also provides helpers for isolating components from vuex getters and actions in form of fakeGetters()
and fakeActions()
. These functions must be called before mounting your component.
fakeGetters()
The fakeGetters()
function returns a sinon stub which can be used to (optionally) control the value which the getter returns.
For example given this component:
const Component = template: '<p></p>' computed: { return this$storegettersfoo } { return this$storegettersbar }
We might test it like so:
fakeActions()
The fakeActions()
function returns a sinon stub which can be used to spy on the action and control what it returns.
For example given this component:
const Component = template: '<p></p>' methods: { return this$store }
We might test it like so:
Examples
More sample usage can be found in the examples/ directory.