NOTE: the linked "duplicate" question & answer does NOT answer my question, please vote to reopen or otherwise explain why this has been closed in comments
I have a created()
hook that calls doSomething()
method. I can get the tests to pass by passing the methods
param to shallowMount()
and overiding with jest.fn()
.
However when I take this approach I receive the deprecation warnings regarding 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
});
}
How can I mock the method called in the created()
hook without passing methods
to shallowMount()
to resolve the deprecation warnings?
Alternatively, is there a way to either mock or bypass the created()
lifecycle hook?
Per the warning suggestion, I realize I could import the method and mock it for tests but I am looking for an alternative especially in cases where that would be overkill.