Skip to content

Commit 677ba31

Browse files
authored
feat: remove deprecation when is is called with component definition (#1676)
1 parent 6e3e4e5 commit 677ba31

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

docs/api/wrapper/is.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
## is
22

33
::: warning Deprecation warning
4-
Using `is` to assert that DOM node or `vm` matches selector is deprecated and will be removed.
4+
Using `is` to assert that wrapper matches DOM selector is deprecated and will be removed.
55

6-
Consider a custom matcher such as those provided in [jest-dom](https://github.com/testing-library/jest-dom#custom-matchers).
6+
For such use cases consider a custom matcher such as those provided in [jest-dom](https://github.com/testing-library/jest-dom#custom-matchers).
77
or for DOM element type assertion use native [`Element.tagName`](https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName) instead.
88

99
To keep these tests, a valid replacement for:
1010

11-
- `is('DOM_SELECTOR')` is a assertion of `wrapper.element.tagName`.
11+
- `is('DOM_SELECTOR')` is an assertion of `wrapper.element.tagName`.
1212
- `is('ATTR_NAME')` is a truthy assertion of `wrapper.attributes('ATTR_NAME')`.
1313
- `is('CLASS_NAME')` is a truthy assertion of `wrapper.classes('CLASS_NAME')`.
1414

15+
Assertion against component definition is not deprecated
16+
1517
When using with findComponent, access the DOM element with `findComponent(Comp).element`
1618
:::
1719

packages/test-utils/src/wrapper.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,18 @@ export default class Wrapper implements BaseWrapper {
321321
}
322322

323323
/**
324-
* Checks if node matches selector
325-
* @deprecated
324+
* Checks if node matches selector or component definition
326325
*/
327326
is(rawSelector: Selector): boolean {
328-
warnDeprecated('is', 'Use element.tagName instead')
329327
const selector = getSelector(rawSelector, 'is')
330328

329+
if (selector.type === DOM_SELECTOR) {
330+
warnDeprecated(
331+
'checking tag name with `is`',
332+
'Use `element.tagName` instead'
333+
)
334+
}
335+
331336
if (selector.type === REF_SELECTOR) {
332337
throwError('$ref selectors can not be used with wrapper.is()')
333338
}

test/specs/wrapper/is.spec.js

+33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { compileToFunctions } from 'vue-template-compiler'
2+
import { config } from 'packages/test-utils/src'
23
import ComponentWithChild from '~resources/components/component-with-child.vue'
34
import Component from '~resources/components/component.vue'
45
import ComponentWithoutName from '~resources/components/component-without-name.vue'
@@ -10,6 +11,17 @@ import {
1011
} from '~resources/utils'
1112

1213
describeWithShallowAndMount('is', mountingMethod => {
14+
let consoleErrorSave
15+
16+
beforeEach(() => {
17+
consoleErrorSave = console.error
18+
console.error = jest.fn()
19+
})
20+
21+
afterEach(() => {
22+
console.error = consoleErrorSave
23+
})
24+
1325
it('returns true if root node matches tag selector', () => {
1426
const compiled = compileToFunctions('<input />')
1527
const wrapper = mountingMethod(compiled)
@@ -133,4 +145,25 @@ describeWithShallowAndMount('is', mountingMethod => {
133145
expect(fn).toThrow(message)
134146
})
135147
})
148+
149+
it('warns when passing a string', () => {
150+
const wrapper = mountingMethod(Component)
151+
152+
config.showDeprecationWarnings = true
153+
wrapper.is('div')
154+
155+
expect(console.error.mock.calls[0][0]).toContain(
156+
'[vue-test-utils]: checking tag name with `is` is deprecated'
157+
)
158+
config.showDeprecationWarnings = false
159+
})
160+
161+
it('does not warn when passing a component definition', () => {
162+
const wrapper = mountingMethod(Component)
163+
164+
config.showDeprecationWarnings = true
165+
wrapper.is(Component)
166+
167+
expect(console.error).not.toHaveBeenCalled()
168+
})
136169
})

0 commit comments

Comments
 (0)