【发布时间】:2021-01-12 05:28:43
【问题描述】:
注意:链接的“重复”问题和答案没有回答我的问题,请投票重新打开或以其他方式解释为什么在 cmets 中关闭了此问题
我有一个调用doSomething() 方法的created() 钩子。我可以通过将methods 参数传递给shallowMount() 并覆盖jest.fn() 来通过测试。
但是,当我采用这种方法时,我会收到关于 methods 的弃用警告:
console.error
[vue-test-utils]: overwriting methods via the `methods` property is deprecated and will be removed in
the next major version. There is no clear migration path for the `methods` property - Vue does not
support arbitrarily replacement of methods, nor should VTU. To stub a complex method extract it from
the component and test it in isolation. Otherwise, the suggestion is to rethink those tests.
TestComponent.Vue:
...
created() {
doSomething();
}
...
methods: {
doSomething(): void { /* do something */ }
}
TestComponent.test.ts:
// mounting method used for tests
function genMount() {
const doSomething = jest.fn();
const el = document.createElement('div');
document.body.appendChild(el);
return shallowMount(TestComponent, {
localVue,
methods: { doSomething }, // deprecated param
store,
mocks,
attachTo: el,
stubs
});
}
如何在不将methods 传递给shallowMount() 的情况下模拟created() 挂钩中调用的方法来解决弃用警告?
或者,有没有办法模拟或绕过 created() 生命周期挂钩?
根据警告建议,我意识到我可以导入该方法并对其进行模拟以进行测试,但我正在寻找一种替代方法,尤其是在这种情况下过度杀伤的情况下。
【问题讨论】:
-
链接的“重复”没有回答我的问题。不知道为什么关闭。
标签: typescript vue.js jestjs vue-test-utils