Skip to content

Support find({ ref }) syntax #716

Closed
Closed
@kanryu

Description

@kanryu

Subject of the issue

Calling findComponent () with the ref method causes a run-time error.

Steps to reproduce

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <button ref="some_ref" v-on:click="$emit('accepted')">OK</button>
  </div>
</template>
import { mount } from '@vue/test-utils';
import Component from '@/components/HelloWorld.vue'

describe('button-exists', () => {
    const wrapper = mount(Component)

    it('ref check', () => {
        const some_ref = wrapper.findComponent({ref: 'some_ref'})
        expect(some_ref.exists()).toBe(true)
    })
})
  • write a template like the one above
  • call wrapper.findComponent({ref: 'some_ref'})

Expected behaviour

findComponent() should return a valid node.

Actual behaviour

runtime error.

    TypeError: Cannot read property 'emits' of undefined

      42 |     it('ref check', () => {
      43 |         console.log(wrapper)
    > 44 |         const some_ref = wrapper.findComponent({ref: 'some_ref'})
         |                                    ^
      45 |         expect(some_ref.exists()).toBe(true)
      46 |     })
      47 | })

      at VueWrapper.Object.<anonymous>.VueWrapper.attachNativeEventListener (node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js:7225:33)
      at new VueWrapper (node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js:7202:15)
      at createWrapper (node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js:7384:12)
      at VueWrapper.findComponent (node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js:7305:24)
      at Object.<anonymous> (tests/App.spec.js:44:36)

Possible Solution

I can't comment because I don't know the detailed implementation of this framework, but when I checked it with the debugger, VueWrapper.prototype.attachNativeEventListener() is executed twice.

The first time, vm points to a Proxy and it runs success.
The second time it points to a HTMLButtonElement and it doesn't have a $options property, which causes a run-time error.

versions

  • "vue": "^3.0.0"
  • "core-js": "^3.6.5",
  • "@vue/compiler-sfc": "^3.0.0",
  • "@vue/component-compiler-utils": "^3.2.2",
  • "@vue/test-utils": "^2.0.0-rc.9",
  • "babel-jest": "<27",
  • "jest": "<27",
  • "vue-jest": "^5.0.0-alpha.10"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions