question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API for testing emitted events

See original GitHub issue

Related to TODO in #4

Some ideas

// Creates listener on the component
wrapper.listenTo('event', spy) 

//  returns array of events that have been emitted
wrapper.events

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

7reactions
yyx990803commented, Sep 27, 2017

Reopen as I am revisiting this feature. After having to manually create spies for all the events I wanted to assert, it felt quite tedious. I am proposing an API that requires minimal setup by simply auto-recording all emitted events in a wrapper.emitted object.

Say you emit the following events:

wrapper.vm.$emit('foo', 1)
wrapper.vm.$emit('foo', 2, 3)
wrapper.vm.$emit('bar')

Then wrapper.emitted would look like this:

{
  foo: [[1], [2, 3]],
  bar: [[]]
}

This simple data structure allows all kinds of assertions:

// assert event emitted
expect(wrapper.emitted.foo).toBeTruthy()

// assert event count
expect(wrapper.emitted.foo.length).toBe(2)

// assert event payload
expect(wrapper.emitted.foo[0]).toEqual([1])

The best part is you don’t need to do any wiring or setup, you can just assert it after you trigger the events.

/cc @eddyerburgh @codebryo

5reactions
yyx990803commented, Jun 8, 2017

What about making it wrapper.on to be consistent with vm.$on? It seems to be just an alias.

Read more comments on GitHub >

github_iconTop Results From Across the Web

emitted | Vue Test Utils
Return an object containing custom events emitted by the Wrapper vm . Returns: { [name: string]: Array<Array<any>> }. Example:.
Read more >
Testing emitted events - Vue Testing Handbook
vue-test-utils provides an emitted API which allows us to make assertions on emitted events. The documentation for emitted is found here .
Read more >
Testing Emitted Events - Unit Testing Vue 3 - Vue Mastery
Learn how to test that your component's custom events were emitted with the correct payload. Unlock this lesson by subscribing to a plan....
Read more >
API | Testing Library
Vue Testing Library re-exports everything from DOM Testing Library. ... inner value while emitting the appropriate native event.
Read more >
What is the best way to test if an event is being emitted to ...
For example, if I have a metric called “Page Viewed” and in my code I run the method ampli.pageViewed(params), after running that method...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found