我在全局注册了一个组件,并在多个文件中使用。 有超过 100 个测试用例文件同时使用了 mount 和 shallowMount,所以我不能去每个测试用例并将 mount 更改为 shallowMount。
有没有什么方法可以全局存根组件,而不是转到每个测试用例并手动存根它。
这可能最好从 Jest setup 文件 中完成,以便为所有测试设置存根:
// jest.config.js
module.exports = {
setupFiles: ['<rootDir>/jest.setup.js'],
}
在设置文件中,您可以使用 Vue Test Utils 全局存根组件
config.global.stubs
:
// jest.setup.js
import { config } from '@vue/test-utils'
// @vue/test-utils@v1
config.stubs['my-component'] = { template: '<div />' }
// @vue/test-utils@v2
config.global.stubs['my-component'] = { template: '<div />' }
存根现在在
config.global.stubs
。我花了将近三个小时才找到答案。