react-testing-library: TypeError: Cannot read property textContent of null when you expect a property on a missing element queried by data-testid
react-testing-libraryversion: 1.0.1nodeversion: n/anpm(oryarn) version: n/a
Relevant code or config
expect(queryByTestId('greeting-text').textContent).toBe('hello there')
What you did:
Did not render the element with 'greeting-text' identifier.
What happened:
TypeError: Cannot read property textContent of null
Reproduction repository:
https://codesandbox.io/s/4q3zol71y9
Problem description:
The error does not describe what has gone wrong.
Suggested solution:
Throw a user-friendly error.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 24 (15 by maintainers)
Commits related to this issue
- feat(debug): Adding debug log when a get call fails (#3) * feat(debug_helper): Adding debug helper when a get call fails * fix(review): Few minor changes * moving pretty-format to the dep * ... — committed to julienw/react-testing-library by antsmartian 6 years ago
- Merge pull request #3 from Gpx/bubble fix: š allow to click elements within labels — committed to lucbpz/react-testing-library by Gpx 6 years ago
Thanks for filing this @sompylasar!
For more context, hereās what the error looks like when I make a typo in a similar test:
The fact that Jest will call out the line number where the error happened makes the problem less concerning for me. I think that people will understand what went wrong.
And just to be clear for everyone, weāre not throwing any errors, this is a regular JavaScript error (trying to read
textContentonnull). I think the error output makes it clear where thatās happening. As to answering why thatās happening, I think that could be improved.For the benefit of everyone else, I talked with @sompylasar about this already. He asked if we could throw an error in
queryByTestIdwhen the element wasnāt found and I explained that would break this use case:Where youāre trying to assert that something does not exist.
So one thingās for sure: We will not be changing the current behavior.
However, it would be nice to improve the error message. One suggestion @sompylasar made is we could create a new function called
queryByTestIdRequired, but first off I doubt many people will use it so we lose the benefits there, and secondly Iād prefer to avoid expanding the API unless itās absolutely necessary.Another idea I had is to create custom expect matchers that people can use in their apps that could throw more helpful error messages:
I donāt think Iāll work on these myself, but I would be happy to accept a contribution that adds this capability with a simple API š
Ok, @pbombās PR was merged. A release should be out soon and youāll be able to use
getByTestIdšAnother suggestion: a generic matcher
where
elis guaranteed to exist or it fails; for safety and clarity, the exceptions from within the matching function can also be caught and enhanced with āthe DOM element did not matchā hint.@sompylasar, if youād like to make a pr to add yourself to the contributor table youāre more than welcome. You can follow the instructions in the CONTRIBUTING.md š
For the record, the link to PR: https://github.com/kentcdodds/react-testing-library/pull/10
@kentcdodds Ahh, that make sense to me. Will do the same and raise a PR. Thanks!
The reason to split it up is so people can just do this in their test files:
Itās explicit, but itās really easy. And it means that if someone wanted to use them differently, or only use one of them, they could:
Gives a bit more flexibility without sacrificing usability.
Another related issue with the
queryByTestIdis with the TypeScript definition. Itās declared that it can returnHTMLElement | nullwhich is true, but prevents me from being able to write the kind of simple code documented for this library. Just like this issue mentions the runtime error if the queried-for element is null, with TypeScript projects, these lines all become compile-time errors because of this possible scenario.The rejected proposal of throwing an error would resolve this issue as well since it would no longer return
null. Alternately, changing the type declaration in an incorrect way to specify that it doesnāt returnnullmight also be a reasonable solution to this problem. Iām not worried about these runtime errors being thrown as this library should only be used in a testing environment where errors thrown are likely to result, correctly, in test failures.What are your thoughts on this problem? I lean towards fudging the type declaration as the developer experience is really bad when
nullcan be returned.Iād rather it be called:
.toSatisfyDOMThereās a
toSatisfyin jest-extend, so it would make sense to follow the naming convention there.