Skip to content

Conversation

dreyks
Copy link
Contributor

@dreyks dreyks commented Feb 10, 2019

What:
expect(null).toBeInTheDocument() fails with a non-descriptive error TypeError: Cannot read property 'ownerDocument' of null

Why:
to produce a more friendly error message

How:

  • when doing a positive expectation do call checkHtmlElement to allow it to fail on null
  • also made negative cases to be more specific since toThrowError() with no arguments passes on any error including logic errors

Checklist:

  • Documentation N/A
  • Tests
  • Updated Type Definitions N/A
  • Ready to be merged
  • Added myself to contributors table

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)

@dreyks dreyks force-pushed the fix-null-to-be-in-the-document branch from 613aeaa to 0f3ae82 Compare February 10, 2019 22:31
@dreyks dreyks changed the title fix: not.toBeInTheDocument fails on null fix: toBeInTheDocument fails on null Feb 10, 2019
@gnapse gnapse merged commit bd2df3a into testing-library:master Feb 11, 2019
@gnapse
Copy link
Member

gnapse commented Feb 11, 2019

Thanks!

@dreyks dreyks deleted the fix-null-to-be-in-the-document branch February 11, 2019 12:11
@gnapse
Copy link
Member

gnapse commented Feb 11, 2019

🎉 This PR is included in version 3.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants