Skip to content

Commit ce2759b

Browse files
committed
feat(config): introduce deprecation warning handler, fix vuejs#1672
Allow passing custom handler for deprecation warnings allowing fine-grained control how these are reported to a user
1 parent bb949a1 commit ce2759b

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

Diff for: docs/api/config.md

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ import { config } from '@vue/test-utils'
1919
config.showDeprecationWarnings = false
2020
```
2121

22+
### `deprecationWarningHandler`
23+
24+
- type: `Function`
25+
26+
Allows fine-grained control on deprecation warnings. When `showDeprecationWarnings` is set to `true`, all deprecation warnings will be passed to this handler with method name as first argument and original message as second.
27+
28+
::: tip
29+
This could be useful to log deprecation messages to separate location or to help in gradual upgrade of codebase to latest version of test utils by ignoring certain deprecated functions warnings
30+
:::
31+
32+
Example:
33+
34+
```js
35+
import { config } from '@vue/test-utils'
36+
37+
config.showDeprecationWarnings = true
38+
config.deprecationWarningHandler = (method, message) => {
39+
if (method === 'emittedByOrder') return
40+
41+
console.error(msg)
42+
}
43+
```
44+
2245
### `stubs`
2346

2447
- type: `{ [name: string]: Component | boolean | string }`

Diff for: packages/shared/util.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,9 @@ export function warnDeprecated(method: string, fallback: string = '') {
105105
if (!config.showDeprecationWarnings) return
106106
let msg = `${method} is deprecated and will be removed in the next major version.`
107107
if (fallback) msg += ` ${fallback}.`
108-
warn(msg)
108+
if (config.deprecationWarningHandler) {
109+
config.deprecationWarningHandler(method, msg)
110+
} else {
111+
warn(msg)
112+
}
109113
}

Diff for: packages/test-utils/types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ interface VueTestUtilsConfigOptions {
166166
provide?: Record<string, any>,
167167
silent?: Boolean,
168168
showDeprecationWarnings?: boolean
169+
deprecationWarningHandler?: Function
169170
}
170171

171172
export declare function createLocalVue (): typeof Vue

Diff for: test/specs/config.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describeWithShallowAndMount('config', mountingMethod => {
1919
config.stubs = configStubsSave
2020
config.silent = configSilentSave
2121
config.methods = {}
22+
config.deprecationWarningHandler = null
2223
console.error = consoleErrorSave
2324
})
2425

@@ -103,6 +104,17 @@ describeWithShallowAndMount('config', mountingMethod => {
103104
expect(wrapper.find('[data-testid="expanded"]').exists()).toEqual(false)
104105
})
105106

107+
it('invokes custom deprecation warning handler if specified in config', () => {
108+
config.showDeprecationWarnings = true
109+
config.deprecationWarningHandler = jest.fn()
110+
111+
const TestComponent = { template: `<div>Test</div>` }
112+
mountingMethod(TestComponent, { attachToDocument: true })
113+
114+
expect(config.deprecationWarningHandler).toHaveBeenCalledTimes(1)
115+
expect(console.error).not.toHaveBeenCalled()
116+
})
117+
106118
it('allows control deprecation warnings visibility for name method', () => {
107119
config.showDeprecationWarnings = true
108120
const Component = {

0 commit comments

Comments
 (0)