Matchers for the emitted event on Vue component
Released jest-matcher-vue-test-utils v1.3.0
Published in
1 min readDec 1, 2018
Added matchers to assert events on the Wrapper object by vue-test-utils.
toEmit
it("emits special event by click", () => {
const wrapper = shallowMount(Component)
const action = () => wrapper.trigger("click") expect(action).toEmit(wrapper, "special")
expect(action).toEmit(wrapper, "special", "clicked")
})
We can assert that the action emits the event (optionally, could check the payload too).
toHaveEmitted
it("emits special event by click", () => {
const wrapper = shallowMount(Component);
wrapper.trigger("click"); expect(wrapper).toHaveEmitted("special");
expect(wrapper).toHaveEmitted("special", "clicked");
});
This just asserts emission logs on wrapper.emitted()
simply.
I would add similar matcher toHaveEmittedNth
to assert not only the single event like the standard matcher toHaveBeenNthCalled
by Jest.
Before releasing without “beta” tag, experimented with the actual Vue project on Indiegogo. Thanks, Super Famous Mike and Josh Dhaliwal for giving important feedback to these new matchers ✨