This book is written for Vue.js 2 and Vue Test Utils v1.

Find the Vue.js 3 version here.

Testing Emitted Events

As applications grow larger, the number of components grows as well. When these components need to share data, child components can emitTesting emitted events - 图1 an event, and the parent component responds.

vue-test-utils provides an emitted API which allows us to make assertions on emitted events. The documentation for emitted is found hereTesting emitted events - 图2.

The source code for the test described on this page can be found hereTesting emitted events - 图3.

Write a Component and Test

Let’s build a simple component. Create an <Emitter> component, and add the following code.

  1. <template>
  2. <div>
  3. </div>
  4. </template>
  5. <script>
  6. export default {
  7. name: "Emitter",
  8. methods: {
  9. emitEvent() {
  10. this.$emit("myEvent", "name", "password")
  11. }
  12. }
  13. }
  14. </script>
  15. <style scoped>
  16. </style>

Add a test called emitEvent:

  1. import Emitter from "@/components/Emitter.vue"
  2. import { mount } from "@vue/test-utils"
  3. describe("Emitter", () => {
  4. it("emits an event with two arguments", () => {
  5. const wrapper = mount(Emitter)
  6. wrapper.vm.emitEvent()
  7. console.log(wrapper.emitted())
  8. })
  9. })

Using the emitted APITesting emitted events - 图4 provided by vue-test-utils, we can easily see the emitted events.

Run the test with yarn test:unit.

  1. PASS tests/unit/Emitter.spec.js
  2. Console
  3. console.log tests/unit/Emitter.spec.js:10
  4. { myEvent: [ [ 'name', 'password' ] ] }

emitted syntax

emitted returns an object. The emitted events are saved as properties on the object. You can inspect the events using emitted().[event]:

  1. emitted().myEvent //=> [ [ 'name', 'password' ] ]

Let’s try calling emitEvent twice.

  1. it("emits an event with two arguments", () => {
  2. const wrapper = mount(Emitter)
  3. wrapper.vm.emitEvent()
  4. wrapper.vm.emitEvent()
  5. console.log(wrapper.emitted().myEvent)
  6. })

Run the test with yarn test:unit:

  1. console.log tests/unit/Emitter.spec.js:11
  2. [ [ 'name', 'password' ], [ 'name', 'password' ] ]

emitted().emitEvent returns an array. The first instance of emitEvent is accessible using with emitted().emitEvent[0]. The arguments are accessible using a similar syntax, emitted().emitEvent[0][0] and so forth.

Let’s make an actual assertion against the emitted event.

  1. it("emits an event with two arguments", () => {
  2. const wrapper = mount(Emitter)
  3. wrapper.vm.emitEvent()
  4. expect(wrapper.emitted().myEvent[0]).toEqual(["name", "password"])
  5. })

The test passes.

Testing events without mounting the component

Some times you might want to test emitted events without actually mounting the component. You can do this by using call. Let’s write another test.

  1. it("emits an event without mounting the component", () => {
  2. const events = {}
  3. const $emit = (event, ...args) => { events[event] = [...args] }
  4. Emitter.methods.emitEvent.call({ $emit })
  5. expect(events.myEvent).toEqual(["name", "password"])
  6. })

Since $emit is just a JavaScript object, you can mock $emit, and by using call to attach it to the this context of emitEvent. By using call, you can call a method without mounting the component.

Using call can be useful in situations where you have some heavy processing in lifecycle methods like created and mounted that you don’t want to execute. Since you don’t mount the component, the lifecycle methods are never called. It can also be useful when you want to manipulate the this context in a specific manner.

Conclusion

  • the emitted API from vue-test-utils is used to make assertions against emitted events
  • emitted is a method. It returns an object with properties corresponding to the emitted events
  • each property of emitted is an array. You can access each instance of an emitted event by using the [0], [1] array syntax
  • the arguments of emitted events are also saved as arrays, and can accessed using the [0], [1] array syntax
  • $emit can be mocked using call, assertions can be made without rendering the component

The source code for the test described on this page can be found hereTesting emitted events - 图5.