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.

Testing emit from $root

See original GitHub issue

Any recommendations on testing functionality that depends on listening to events emitted via $root.$emit? Like you would via wrapper.vm.$root.$emit for example using vue-test-utils. Haven’t been able to find anything in the docs.

Thanks for a great library!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jskogelincommented, Nov 11, 2020

Let me know if this is a feature the library needs and I’ll send in a PR!

0reactions
jskogelincommented, Nov 16, 2020

Can you just wrap the component?

Thank you! Can’t believe I didn’t think of this sooner. This was my solution:

it('should respond to root emit', function () {
    const Wrapper = {
      mounted () {
        this.$root.$emit('emit-to-test')
      },

      render (h) {
        return h('div', [ h(ComponentToTest) ])
      }
    }

    // ...assert stuff
})

I also noticed $on no longer exists in Vue 3, you might want to keep this in mind.

Thanks for the heads up!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing this.$root.$emit with wrapper.emitted #980 - GitHub
In version 1.0.0-beta.16 , the wrapper.emitted('say') test passes for the event emitted on the component's root. What is ...
Read more >
How to test events on the root element of component in vue 2?
Another possibility it's to find your element in the dom and check the emitted value of your root component.
Read more >
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
Some times you might want to test emitted events without actually mounting the component. You can do this by using call . Let's...
Read more >
Emitting events on root component in Vue - CodeSandbox
Emitting events on root component in Vue. 0. Embed Fork Create Sandbox Sign in. Sandbox Info ... Tests. (0.46x). Mobile. Console.
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