-
Notifications
You must be signed in to change notification settings - Fork 466
Closed
Labels
help wantedExtra attention is neededExtra attention is neededneeds discussionWe need to discuss this to come up with a good solutionWe need to discuss this to come up with a good solution
Description
dom-testing-libraryversion: 1.1.0nodeversion: N/Anpm(oryarn) version: N/A
Relevant code or config
(copy of the linked code)
function checkHtmlElement(htmlElement) {
if (!(htmlElement instanceof HTMLElement)) {
throw new Error(
`The given subject is a ${getDisplayName(
htmlElement,
)}, not an HTMLElement`,
)
}
}
// ...
const extensions = {
toBeInTheDOM(received) {
if (received) {
checkHtmlElement(received)
}
return {
pass: !!received,
message: () => {
const to = this.isNot ? 'not to' : 'to'
return getMessage(
matcherHint(
`${this.isNot ? '.not' : ''}.toBeInTheDOM`,
'element',
'',
),
'Expected',
`element ${to} be present`,
'Received',
received,
)
},
}
},What you did:
- Used
.toBeInTheDOM()in a test with an element disconnected from the DOM, like here; the test passed, surprisingly. - Reviewed the source code of
jest-extensions.js
What happened:
Discovered that toBeInTheDOM does not check that the element is attached to a full DOM tree.
Reproduction repository:
Problem description:
toBeInTheDOM, according to its name, has to ensure that the given element is in the DOM, i.e. attached to the full DOM tree, not hanging as a standalone element or in a standalone subtree.
Suggested solution:
Find out if the element is actually attached to a DOM document via element.ownerDocument.contains(element), and report an assertion failure if it's not.
Metadata
Metadata
Assignees
Labels
help wantedExtra attention is neededExtra attention is neededneeds discussionWe need to discuss this to come up with a good solutionWe need to discuss this to come up with a good solution