Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Jest测试具有子组件的Vue组件_Javascript_Vue.js_Testing_Jestjs_Components - Fatal编程技术网

Javascript 如何使用Jest测试具有子组件的Vue组件

Javascript 如何使用Jest测试具有子组件的Vue组件,javascript,vue.js,testing,jestjs,components,Javascript,Vue.js,Testing,Jestjs,Components,我是前端测试新手,现在我花了两天多的时间尝试测试一个组件,但没有取得任何进展 我的组件CreateStoryForm如下所示: <template> <v-card> <v-card-title>Title</v-card-title> <v-card-text> <v-form> <base-btn-text @click="cancel(

我是前端测试新手,现在我花了两天多的时间尝试测试一个组件,但没有取得任何进展

我的组件CreateStoryForm如下所示:

<template>
  <v-card>
    <v-card-title>Title</v-card-title>
    <v-card-text>
      <v-form>
        <base-btn-text
          @click="cancel()"
          class=cancel
        >
          Cancel
        </base-btn-text>
      </v-form>
    </v-card-text
  </v-card>
</template>

<script>
  ...
  methods: {
    cancel() {
        this.$emit('cancel');
    },
  }
</script>
现在我想用Jest测试表单组件,看看在按下按钮时是否发出事件“cancel”。但是,我只能在使用默认按钮标记而不是自定义BaseButtonText组件时使其工作

我尝试的是:

  • 使用存根:

     wrapper = shallowMount(CreateStoryForm, {
       ...
       stubs: {
         'base-btn-text': true
      }
    
  • ->
    wrapper.find('base-btn-text-stub')。trigger('click')不发出事件

  • 将存根与实现一起使用:

     const CancelButton = "<button @click='cancel()'>Cancel</button>"
     ...
     wrapper = shallowMount(CreateStoryForm, {
       ...
       stubs: {
         'base-btn-text': CancelButton
      }
    
  • ->
    wrapper.find(BaseButtonText.trigger('click')不发出事件

    现在我完全迷路了

     const CancelButton = "<button @click='cancel()'>Cancel</button>"
     ...
     wrapper = shallowMount(CreateStoryForm, {
       ...
       stubs: {
         'base-btn-text': CancelButton
      }
    
     import BaseButtonText ...
     Vue.component('base-btn-text, BaseButtonText);