It would be useful to be able to mock $refs.

Currently, as far as I understand there is no way to test a method that calls a childs methods via $refs.

methodToTest ( ) {
  this.a = 1
  this.$refs.childComponent.childsMethod()
}
---
undefined is not an object (evaluating 'this.$refs.childComponent.childsMethod')

Issue Analytics

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

github_iconTop GitHub Comments

69reactions
eddyerburghcommented, Jan 5, 2018

Hi @ddykhoff, thanks for the fiddle.

The reason you can’t call the method on the ref is because shallow stubs all the child components. The $refs object is still populated, but the toast component doesn’t have any methods (because it’s been stubbed).

You can use mount instead of shallow if you want to interact with a child components methods. Alternatively, you could use shallow and pass a custom stub:

const VueToastStub = {
  render: () => {},
  methods: {
  	setOptions: () => {}
  }
}

const wrapper = shallow(Approvals, {
  stubs: {
    'vue-toast': VueToastStub
  }
})
33reactions
booomerangcommented, May 26, 2020

For me helped the way of changing shallowMount to mount, and mocking the ref method like this:

const mockedMethod = jest.fn()
wrapper.vm.$refs.childComponent.someMethod = mockedMethod

// link where parent method calls this.$refs.childComponent.someMethod
link.trigger('click')

await flushPromises()

expect(mockedMethod).toHaveBeenCalled()

Hope this may be useful for someone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue jest $refs function and component mocking issue
I'm trying to mock the library and write the test but i'm getting a this.$refs.headroom._setHeightOffset is not a function it seems like the ......
Read more >
How to stub named ref in vue test
Hi I need help to mock/stub something like this.$refs.nameNotComponent.focus() How I can achieve this? nameNotComponent is a name I choosed ...
Read more >
Mock Ref return values and ref locals | Telerik JustMock - Telerik
JustMock's powerful capabilities enable you to mock methods with ref return references in order to test your code in perfect isolation.
Read more >
chromium / external / github.com / golang / mock - Google Git
GoMock is a mocking framework for the Go programming language. It integrates well with Go's built-in testing package, but can be used in...
Read more >
How to mock a same flow-ref (or) sub-flow with different ...
I have a mule flow in which there are two flow-refs referring to same sub-flow. I have to mock those two flow-refs with...
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