首页 > 解决方案 > 运行单元测试时出现“Vue 类型不存在属性‘扩展’”错误

问题描述

我创建了一个 Vue 组件和一个单元测试来验证它的行为。我可以在我的 Vue 应用程序上使用该组件而没有任何问题,但是当我使用 jest 运行我的单元测试时,我得到了错误:

error TS2339: Property 'extend' does not exist on type 'typ
eof import("</path/to/module>")'.

    1 export default Vue.extend({

vButton.ts - 被测组件

export default Vue.extend({
  template: `
  <div>
    <button class="default-button" @click="click">
      <span>{{ text }}</span>
    </button>
  </div>
  `,
  props: {
    text: String,
    action: Function
  },

  methods: {
    click(): void {
      this.$emit('action');
    }
  }
}); 

vButton.spec.ts - 单元测试

import { mount } from '@vue/test-utils'
import vButton from '../views/core/ts/components/vButton'

describe('vButton', () => {
  describe(':props', () => {
    it(':text - should render a button with the passed-in label text', () => {
      const msg = 'new message';

      const wrapper = mount(vButton, {
        propsData: { text: msg },
      });

      expect(wrapper.text()).toMatch(msg);
    });
  });

  describe('@events', () => {
    it('@click - should emit an "action" event when the button is clicked', () => {
      const wrapper = mount(vButton);
      const button = wrapper.find('button')

      button.trigger('click')

      expect(wrapper.emitted().action).toBeTruthy()
    })
  });
});

我希望 Vue.extend 在运行单元测试时能够正常工作。

标签: unit-testingvue.jsjestjsvue-component

解决方案


推荐阅读