Skip to content

Commit 767523a

Browse files
committed
refactor: improve migration path for setMethods and options.methods
1 parent cda9f8c commit 767523a

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

docs/api/wrapper-array/setMethods.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
::: warning Deprecation warning
44
`setMethods` is deprecated and will be removed in future releases.
5+
6+
There's no clear path to replace `setMethods`, because it really depends on your previous usage. It easily leads to flaky tests that rely on implementation details, which [is discouraged](<(https://github.com/vuejs/rfcs/blob/668866fa71d70322f6a7689e88554ab27d349f9c/active-rfcs/0000-vtu-api.md#setmethods)>).
7+
8+
Thus, we suggest to to rethink those tests using the tools Vue Test Utils provides.
59
:::
610

711
Sets `Wrapper` `vm` methods and forces update on each `Wrapper` in `WrapperArray`.

docs/api/wrapper/setMethods.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
::: warning Deprecation warning
44
`setMethods` is deprecated and will be removed in future releases.
5+
6+
There's no clear path to replace `setMethods`, because it really depends on your previous usage. It easily leads to flaky tests that rely on implementation details, which [is discouraged](<(https://github.com/vuejs/rfcs/blob/668866fa71d70322f6a7689e88554ab27d349f9c/active-rfcs/0000-vtu-api.md#setmethods)>).
7+
8+
Thus, we suggest to to rethink those tests using the tools Vue Test Utils provides.
59
:::
610

711
Sets `Wrapper` `vm` methods and forces update.

packages/shared/merge-options.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export function mergeOptions(
3535
[key: string]: Function
3636
})
3737
if (methods && Object.keys(methods).length) {
38-
warnDeprecated('overwriting methods via the `methods` property')
38+
warnDeprecated(
39+
'overwriting methods via the `methods` property',
40+
'There is no clear migration path for the `methods` property - Vue does not support arbitrarily replacement of methods, nor should VTU. If you need to stub out a method, extract it out and test it. Otherwise, the suggestion is to rethink those tests'
41+
)
3942
}
4043

4144
const provide = (getOption(options.provide, config.provide): Object)

packages/test-utils/src/wrapper.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,10 @@ export default class Wrapper implements BaseWrapper {
632632
* @deprecated
633633
*/
634634
setMethods(methods: Object): void {
635-
warnDeprecated(`setMethods`)
635+
warnDeprecated(
636+
`setMethods`,
637+
`There is no clear migration path for setMethods - Vue does not support arbitrarily replacement of methods, nor should VTU. If you need to stub out a method, extract it out and test it. Otherwise, the suggestion is to rethink those tests`
638+
)
636639

637640
if (!this.vm) {
638641
throwError(`wrapper.setMethods() can only be called on a Vue instance`)

test/specs/config.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,58 @@ describeWithShallowAndMount('config', mountingMethod => {
144144
)
145145
})
146146
})
147+
148+
describe('methods overriding deprecation warning', () => {
149+
const expectedErrorMessage = `There is no clear migration path`
150+
const Component = {
151+
template: '<div></div>',
152+
methods: {
153+
foo() {}
154+
}
155+
}
156+
157+
it('should show warning for options.methods if config is enabled', () => {
158+
config.showDeprecationWarnings = true
159+
160+
mountingMethod(Component, {
161+
methods: { foo: () => {} }
162+
})
163+
164+
expect(console.error).to.be.calledWith(
165+
sandbox.match(expectedErrorMessage)
166+
)
167+
})
168+
169+
it('should not show warning for options.methods if config is disabled', () => {
170+
config.showDeprecationWarnings = false
171+
172+
mountingMethod(Component, {
173+
methods: { foo: () => {} }
174+
})
175+
176+
expect(console.error).not.to.be.calledWith(
177+
sandbox.match(expectedErrorMessage)
178+
)
179+
})
180+
181+
it('should show warning for setMethods if config is enabled', () => {
182+
config.showDeprecationWarnings = true
183+
184+
mountingMethod(Component).setMethods({ foo: () => {} })
185+
186+
expect(console.error).to.be.calledWith(
187+
sandbox.match(expectedErrorMessage)
188+
)
189+
})
190+
191+
it('should not show warning for setMethods if config is disabled', () => {
192+
config.showDeprecationWarnings = false
193+
194+
mountingMethod(Component).setMethods({ foo: () => {} })
195+
196+
expect(console.error).not.to.be.calledWith(
197+
sandbox.match(expectedErrorMessage)
198+
)
199+
})
200+
})
147201
})

0 commit comments

Comments
 (0)