Vue Test Utils-跳过创建的挂钩 [英] Vue Test Utils - Skip created hook
问题描述
我想跳过created()
挂钩中正在调用的所有方法.有办法吗?
I want to skip all of the methods that are being called within the created()
hook. Is there a way to do this?
所以代替这个
created() {
this.getAllocations();
this.getModels();
this.getTeams();
this.getCustodians();
this.getDefaultFeeStructure();
}
我想要这个
created() { }
值得注意的是,我实际上不能更改组件本身,但是出于测试目的,这需要完成.
It's worth noting, I cannot actually change the component itself, but for testing purposes, this needs to be done.
推荐答案
You can accomplish this with a global mixin (see https://vuejs.org/v2/guide/mixins.html#Global-Mixin)
但是,对于您的情况,您需要一个自定义合并策略来防止运行在组件上创建的钩子:
However, for your case you need a custom merge strategy to prevent the created hook on the component from being run:
具有相同名称的挂钩函数将合并到一个数组中,以便将全部调用它们. Mixin挂钩将在组件自己的挂钩之前被调用. ( https://vuejs.org/v2/guide/mixins.html#Option -合并)
在 https://jsfiddle.net/rushimusmaximus/9akf641z/3/
Vue.mixin({
created() {
console.log("created() in global mixin")
}
});
const mergeCreatedStrategy = Vue.config.optionMergeStrategies.created;
Vue.config.optionMergeStrategies.created = (parent, child) => {
return mergeCreatedStrategy(parent);
};
new Vue ({
el: "#vue-app",
template: '<p>See console output for logging. Rendered at {{renderDate}}</p>',
data() {
return {
renderDate: new Date()
}
},
created() {
console.log("created() in component")
}
})
这篇关于Vue Test Utils-跳过创建的挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!