【问题标题】:How to globally stub component in vue test utils jest?如何在vue test utils jest中全局存根组件?
【发布时间】:2021-09-19 19:15:30
【问题描述】:

我已经全局注册了一个组件并在多个文件中使用过。 有超过 100 个测试用例文件同时使用了 mount 和 shallowMount,所以我不能去每个测试用例并将 mount 更改为 shallowMount。

有什么方法可以全局存根组件,而不是去每个测试用例手动存根。

【问题讨论】:

    标签: vue.js jestjs stub testcase vue-test-utils


    【解决方案1】:

    这可能最好通过 Jest setup file 完成,以便为所有测试设置存根:

    // jest.config.js
    module.exports = {
      setupFiles: ['<rootDir>/jest.setup.js'],
    }
    

    在设置文件中,您可以使用 Vue Test Utils 的 config.stubs 全局存根组件:

    // jest.setup.js
    import { config } from '@vue/test-utils'
    
    config.stubs['my-component'] = '<div />'
    

    【讨论】:

      猜你喜欢
      • 2019-04-26
      • 2019-03-28
      • 2020-06-08
      • 2018-07-07
      • 2019-02-10
      • 1970-01-01
      • 2019-03-11
      • 2021-11-13
      • 2019-03-17
      相关资源
      最近更新 更多