Skip to content

feat(config): introduce deprecation warning handler, fix #1672 #1700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ import { config } from '@vue/test-utils'
config.showDeprecationWarnings = false
```

### `deprecationWarningHandler`

- type: `Function`

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.

::: tip
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
:::

Example:

```js
import { config } from '@vue/test-utils'

config.showDeprecationWarnings = true
config.deprecationWarningHandler = (method, message) => {
if (method === 'emittedByOrder') return

console.error(msg)
}
```

### `stubs`

- type: `{ [name: string]: Component | boolean | string }`
Expand Down
6 changes: 5 additions & 1 deletion packages/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,9 @@ export function warnDeprecated(method: string, fallback: string = '') {
if (!config.showDeprecationWarnings) return
let msg = `${method} is deprecated and will be removed in the next major version.`
if (fallback) msg += ` ${fallback}.`
warn(msg)
if (config.deprecationWarningHandler) {
config.deprecationWarningHandler(method, msg)
} else {
warn(msg)
}
}
1 change: 1 addition & 0 deletions packages/test-utils/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ interface VueTestUtilsConfigOptions {
provide?: Record<string, any>,
silent?: Boolean,
showDeprecationWarnings?: boolean
deprecationWarningHandler?: Function
}

export declare function createLocalVue (): typeof Vue
Expand Down
12 changes: 12 additions & 0 deletions test/specs/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describeWithShallowAndMount('config', mountingMethod => {
config.stubs = configStubsSave
config.silent = configSilentSave
config.methods = {}
config.deprecationWarningHandler = null
console.error = consoleErrorSave
})

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

it('invokes custom deprecation warning handler if specified in config', () => {
config.showDeprecationWarnings = true
config.deprecationWarningHandler = jest.fn()

const TestComponent = { template: `<div>Test</div>` }
mountingMethod(TestComponent, { attachToDocument: true })

expect(config.deprecationWarningHandler).toHaveBeenCalledTimes(1)
expect(console.error).not.toHaveBeenCalled()
})

it('allows control deprecation warnings visibility for name method', () => {
config.showDeprecationWarnings = true
const Component = {
Expand Down