fix: toBeInTheDocument fails on null #79
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What:
expect(null).toBeInTheDocument()
fails with a non-descriptive errorTypeError: Cannot read property 'ownerDocument' of null
Why:
to produce a more friendly error message
How:
null
toThrowError()
with no arguments passes on any error including logic errorsChecklist:
Alternatively instead of calling checkHtmlElement we can change https://github.com/gnapse/jest-dom/blob/02c340d03f5b9bb5b02c51286f306ddd1759ae42/src/to-be-in-the-document.js#L26 to something like
${stringify(element ? element.ownerDocument.cloneNode(false) : 'document')}
. Tell me if that's preferable (it also makes the error message even more obvious)