jest-matcher-vue-test-utils
Cute matchers for Jest to test Vue components with Vue Test Utils.
You can write tests for Vue component/store intuitively ⚡️
it("Emits 'select' event by clicking PrimaryButton", () => {
const wrapper = shallowMount(Component);
expect(wrapper.emitted().select).toBeUndefined();
wrapper.find(PrimaryButton).vm.$emit("click");
expect(wrapper.emitted().select[0]).toBeTruthy();
});
becomes
it("Emits 'select' event by clicking PrimaryButton", () => {
const wrapper = shallowMount(Component);
expect(() => {
wrapper.find(PrimaryButton).vm.$emit("click");
}).toEmit(wrapper, "select");
});
And all matchers have type definition and doc 💇♂️
Installation
Get from npm:
$ npm install -D jest-matcher-vue-test-utils
Then, register matchers on your jest process:
import vueTestUtilMatchers from "jest-matcher-vue-test-utils";
expect.extend({ ...vueTestUtilMatchers });
Provided Matchers
Existence on Wrapper
toShow
Assert the function shows a content on Wrapper of vue-test-utils
<template>
<div>
<p v-if="isError" class="error">message</p>
</div>
</template>
...
data: function () {
return {
isError: false
}
},
methods: {
showError () {
this.isError = true;
}
}
import Component from "./error-message.vue";
it("show error by showError", async () => {
return expect(async () => {
wrapper.vm.showError();
await wrapper.vm.$nextTick();
}).toShow(wrapper, "p.error");
});
toHide
Assert the function hides a content on Wrapper of vue-test-utils
<template>
<div>
<p v-if="isError" class="error">message</p>
</div>
</template>
...
data: function () {
return {
isError: true
}
},
methods: {
hideError () {
this.isError = false;
}
}
import Component from "./error-message.vue";
it("show error by showError", async () => {
return expect(async () => {
wrapper.vm.hideError();
await wrapper.vm.$nextTick();
}).toHide(wrapper, "p.error");
});
Events on Wrapper
toEmit
Assert the action emits the event (with the payload optionally) on Wrapper of vue-test-utils
<template>
<div @click="emitEvent('clicked')">
Click Me
</div>
</template>
<script>
module.exports = {
methods: {
emitEvent (e) {
this.$emit("special", e);
}
}
}
</script>
import Component from "./event.vue";
it("emits special event by click", () => {
const wrapper = shallowMount(Component);
expect(() => wrapper.trigger("click")).toEmit(wrapper, "special");
expect(() => wrapper.trigger("click")).toEmit(wrapper, "special", "clicked");
});
Async function is supported as well.
it("emits special event by click", async () => {
const wrapper = shallowMount(Component);
return expect(async () => triggersEventAsynchronously()).toEmit(wrapper, "special", "clicked");
});
toHaveEmitted
Assert the event is emitted (with the payload optionally) on Wrapper of vue-test-utils
<template>
<div @click="emitEvent('clicked')">
Click Me
</div>
</template>
<script>
module.exports = {
methods: {
emitEvent (e) {
this.$emit("special", e);
}
}
}
</script>
import Component from "./event.vue";
it("emits special event by click", () => {
const wrapper = shallowMount(Component);
wrapper.trigger("click");
expect(wrapper).toHaveEmitted("special");
expect(wrapper).toHaveEmitted("special", "clicked");
});
Vuex actions/mutations
toDispatch
Assert the function dispatches Vuex action on the component
<template>
<div @click="dispatchStore('click')">
Click Me
</div>
</template>
<script>
module.exports = {
methods: {
dispatchStore (e) {
this.$store.dispatch('awesomeAction', e);
}
}
}
</script>
import Component from "./click-store.vue";
it("Dispatches the action on store by click", () => {
const wrapper = shallowMount(Component);
expect(() => {
wrapper.trigger("click");
}).toDispatch(wrapper, "awesomeAction");
expect(() => {
wrapper.trigger("click");
}).toDispatch(wrapper, "awesomeAction", 'click');
});
Async function is supported as well.
it("dispatches the action on store by click", async () => {
return expect(async () => {
dispatchEventAsynchronosly();
}).toDispatch(wrapper, "awesomeAction", 'click');
});
toCommit
(TBD)
Assert the store mutation is committed
<template>
<div @click="commitStore('click')">
Click Me
</div>
</template>
<script>
module.exports = {
methods: {
commitStore (e) {
this.$store.commit('importantMutation', e);
}
}
}
</script>
import Component from "./click-store.vue";
it("Commits the mutation on store by click", () => {
const wrapper = shallowMount(Component);
expect(() => {
wrapper.trigger("click");
}).toCommit(wrapper, "importantMutation");
expect(() => {
wrapper.trigger("click");
}).toCommit(wrapper, "importantMutation", 'click');
});
toHaveDispatched
Assert a component has dispatched Vuex action
<template>
<div @click="dispatchStore('click')">
Click Me
</div>
</template>
<script>
module.exports = {
methods: {
dispatchStore (e) {
this.$store.dispatch('awesomeAction', e);
}
}
}
</script>
import Component from "./click-store.vue";
import { vuexPlugin } from "jest-matcher-vue-test-utils";
it("Dispatches the action on store by click", () => {
const store = new Vuex.Store({
actions: dispatchStore() {},
plugins: [vuexPlugin()]
});
const wrapper = shallowMount(Component, { store })
wrapper.trigger("click");
expect(wrapper).toHaveDispatched("awesomeAction");
expect(wrapper).toHaveDispatched("awesomeAction", "click");
});
Prop Validations
toBeValidProps
Assert that a prop set is valid for a component
props: {
name: {
type: String,
required: true
}
fullname: {
validator: function (val) {
return !!val && val.match(/.+\s.+/);
}
}
}
import Component from "./name-require-and-fullname-is-validated-component.vue";
it("component validates props", () => {
expect(Component).toBeValidProps({ name: "required name", fullName: "Kengo Hamasaki" });
expect(Component).toBeValidProps({ fullName: "Kengo Hamasaki" });
expect(Component).toBeValidProps({ name: "required name", fullName: "Kengo" });
});
toBeValidProp
Assert that a single prop is valid for a component
props: {
name: {
type: String,
required: true
}
}
import Component from "./name-require-component.vue";
it("component validates props", () => {
expect(Component).toBeValidProp("name", "Required Name");
expect(Component).toBeValidProp("name", null);
expect(Component).toBeValidProp("name", 123});
});
toRequireProp
Assert that a component requires a prop
props: {
name: {
type: String,
required: true
}
}
import Component from "./name-require-component.vue";
it("component requires name prop", () => {
expect(Component).toRequireProp("name");
expect(Component).toRequireProp("birthday");
});
toHaveDefaultProp
Assert that a component gives default to a prop
props: {
address: {
type: String,
default: "Kitakyushu, Japan"
}
}
import Component from "./default-address-component.vue";
it("component gives default value for address prop", () => {
expect(Component).toHaveDefaultProp("address", "Kitakyushu, Japan");
expect(Component).toHaveDefaultProp("address", "San Francisco, US");
});
toBeValidPropWithTypeCheck
Assert that a component validates a prop with type
props: {
zipcode: {
type: String
}
}
import Component from "./takes-zipcode-component.vue";
it("component validates zipcode prop", () => {
expect(Component).toBeValidPropWithTypeCheck("zipcode", "94103");
expect(Component).toBeValidPropWithTypeCheck("zipcode", 94103);
});
toBeValidPropWithCustomValidator
Assert that a component validates a prop with custom validator
props: {
fullname: {
validator: function (val) {
return !!val && val.match(/.+\s.+/);
}
}
}
import Component from "./fullname-is-validated-component.vue";
it("component validates fullname prop", () => {
expect(Component).toBeValidPropWithCustomValidator("fullname", "Kengo Hamasaki");
expect(Component).toBeValidPropWithCustomValidator("fullname", "Kengo");
});
Config
We can configure the matchers. Currently accepting mountOptions property to give options for shallowMount
which is running in inside of matchers.
import vueTestUtilMatchers, { config } from "jest-matcher-vue-test-utils";
import { createLocalVue } from "@vue/test-utils";
config({
mountOptions: { localVue: createLocalVue() }
});
License
MIT, Copyright (c) 2018- Kengo Hamasaki