You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
toBeInTheDOM matcher only ensures instanceof HTMLElement, expected to check that the element is attached to a full DOM tree #9
functioncheckHtmlElement(htmlElement){if(!(htmlElementinstanceofHTMLElement)){thrownewError(`The given subject is a ${getDisplayName(htmlElement,)}, not an HTMLElement`,)}}// ...constextensions={toBeInTheDOM(received){if(received){checkHtmlElement(received)}return{pass: !!received,message: ()=>{constto=this.isNot ? 'not to' : 'to'returngetMessage(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.
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.
toBeInTheDOM can also optionally accept a container element which should be used instead of the ownerDocument (the container should have the same ownerDocument).
I did notice this inconsistency while working in #1. Essentially .toBeInTheDOM is a glorified .toBeInstanceOfClass(HTMLElement).
The weird thing about adding a container argument is that when using the query* selectors in this library, you're pretty much guaranteed that if something is returned, it'll be in "the DOM".
expect(queryByText(container,'OK')).toBeInTheDOM(container);// The same happens with toBeAChildOfexpect(queryByText(container,'OK')).toBeAChildOf(container)
The only way the above expectations can fail is if the element is not found, and therefore null. There's no way it can exist being an HTMLElement, and not be in "the DOM".
However, I also agree that this does not solve the problem of this custom matcher not really doing what it is supposed to do. And there's no guarantee that it has to always be used with these query* selectors.
Activity
sompylasar commentedon Apr 8, 2018
toBeInTheDOMcan also optionally accept acontainerelement which should be used instead of theownerDocument(thecontainershould have the sameownerDocument).kentcdodds commentedon Apr 8, 2018
That sounds good to me. Perhaps we should deprecate the current one and add a new one in its place. Something like this?
Either that or do as you suggested and just make it an optional argument of
toBeInTheDOM. I think I'll be easy to sway either way.antsmartian commentedon Apr 8, 2018
@sompylasar Agree, that's bug to me. To me we can keep the same name as
toBeInTheDOM, but accepts the optional argument too.gnapse commentedon Apr 8, 2018
I did notice this inconsistency while working in #1. Essentially
.toBeInTheDOMis a glorified.toBeInstanceOfClass(HTMLElement).The weird thing about adding a container argument is that when using the
query*selectors in this library, you're pretty much guaranteed that if something is returned, it'll be in "the DOM".The only way the above expectations can fail is if the element is not found, and therefore
null. There's no way it can exist being an HTMLElement, and not be in "the DOM".However, I also agree that this does not solve the problem of this custom matcher not really doing what it is supposed to do. And there's no guarantee that it has to always be used with these
query*selectors.sompylasar commentedon Apr 8, 2018
Here's where I needed it to fail if the element happened to not be appended because of a bug: https://github.com/sompylasar/dom-testing-library/blob/5c4061834cf96bd8b878bf1a0ccc355da7d4c42e/src/__tests__/waitForElements.js#L94
I checked for parentNode, but for more deep tree it would be more convenient with a custom assertion.
gnapse commentedon Apr 11, 2018
Reopened this issue in testing-library/jest-dom#3 so we can continue the discussion over there.
sompylasar commentedon Apr 11, 2018
@gnapse Thanks, we can close this one then in favor of yours testing-library/jest-dom#3
fix(TypeScript): add missing 'typings' folder to files property (test…