【问题标题】:Vue3 with Jest stub functionality does not stub具有 Jest 存根功能的 Vue3 不会存根
【发布时间】:2021-08-16 19:12:16
【问题描述】:

组件:

<template>
  <div id="fileviewer" class="min-h-full">
    <section class="gap-4 mt-4">
      <div class="bg-medium-50 w-1/3 p-4">
        <FileUpload ></FileUpload>
      </div>
      <div class="bg-medium-50 w-2/3 p-4">
        <FileViewer></FileViewer>
      </div>
    </section>
  </div>
</template>

<script>
import FileUpload from "@/components/FileUpload";
import FileViewer from "@/components/FileViewer";

export default {
  name: "FileManager",
  components: { FileUpload, FileViewer },
};
</script>

测试:

import { mount } from "@vue/test-utils";
import FileManager from '@/views/FileManager';

describe('FileManager.vue', () =>{

  it('should mount', () => {
    const wrapper = mount(FileManager, {
      global: {
        stubs: {
          FileUpload: true,
          FileViewer: true
        }
      }
    })

    expect(wrapper).toBeDefined()
  })

})

根据docs,不适用于我。无特殊装置。相反,框架想要为子组件执行“导入”语句,然后失败,因为我不想为这个组件模拟“获取”。有什么想法吗?

"vue-jest": "^5.0.0-alpha.9"
"@vue/test-utils": "^2.0.0-rc.6"
"vue": "^3.0.0",

感谢您的帮助。

【问题讨论】:

    标签: unit-testing jestjs vuejs3 vue-test-utils


    【解决方案1】:

    。如果你想自动存根所有子组件,你可以使用shallowMount 而不是mount

    。如果您想这样使用mount,请尝试像这样修复您的存根:

    global: {
      stubs: {
        FileUpload: {
          template: '<div class="file-upload-or-any-class-you-want">You can put there anything you want</div>'
        },
        FileViewer: {
          template: '<div class="file-viewer-or-any-class-you-want">You can put there anything you want</div>'
        }
      }
    }
    
    
    

    或者您可以像我一直做的那样在测试之前定义您的存根。例如:

    const FileUploadStub = {
      template: '<div class="file-upload-or-any-class-you-want">You can put there anything you want</div>'
    }
    
    const FileViewerStub: {
      template: '<div class="file-viewer-or-any-class-you-want">You can put there anything you want</div>'
    }
    

    然后在mountshallowMount 中使用存根:

    global: {
      stubs: {
        FileUpload: FileUploadStub,
        FileViewer: FileViewerStub
      }
    }
    

    【讨论】:

    • 谢谢。对我来说的问题是,虽然我使用了 shallowMount,但框架仍然会在子组件中安装导入(例如,测试会失败,因为尽管我使用了 shallowMount,但它试图导入 firebase。
    猜你喜欢
    • 2017-04-09
    • 2014-08-08
    • 2018-08-02
    • 2020-03-31
    • 1970-01-01
    • 2021-01-10
    • 2017-12-28
    • 1970-01-01
    • 2011-12-26
    相关资源
    最近更新 更多