Potentially wrong linting suggestion in jest-dom/prefer-in-document
- "eslint-plugin-jest-dom": "^4.0.1",
nodeversion: 14.18.0npmversion: 8.5.2
Relevant code or config
// Here I know the notification should not be in the document. I tried to use `findByLabelText` and it throws an error,
// so I thought about using `queryByLabelText` to assert that it's correctly null
let notification = screen.queryByLabelText("notification address copied");
// Using expect.toBeDefined() throws an error, despite being suggested by eslint as better solution
// expect(notification).toBeDefined();
// Need to disable the line to avoid linting errors
expect(notification).toBeNull(); // eslint-disable-line jest-dom/prefer-in-document
What you did:
- check code and comments above
What happened:
- I got a linting error suggesting to use
toBeInTheDocumentto assert existence of a node, but in this case I am usingqueryByLabelTextand therefore I cannot usetoBeInTheDocumentto assert a null value.
Problem description:
- Check "What happened" section above
Suggested solution:
- Do not suggest to use
toBeInTheDocumentif user is querying withqueryBy*
I think the suggestion is to use toBeInTheDocument with the negation property .not
expect(screen.queryByLabelText("notification address copied")).not.toBeInTheDocument()
Which personally, it reads better than expect(screen.queryByLabelText("notification address copied")).toBeNull()
I just ran into this issue as well and will need to turn this rule off. It doesn't make sense to be asserting that null is not within the document.
expect(null).not.toBeInTheDocument()
@twdrake-ap sure but the code you've given doesn't make sense anyway - why would you assert that a literal null is not present in the document?
This situation is specifically called out by the docs as when you should use not.toBeInTheDocument, using screen.queryByLabelText.
@G-Rath That is my point, the code snippet is what OP's example would evaluate to if he used this package's default lint rules. Why does it make sense to assert that a query result, that we are expecting to return null, not be in the document instead of asserting that is IS null?