Jest-dom: `.not.toBeInTheDocument()` fails when the element you are matching against isn't in the document

Created on 10 Feb 2020  路  2Comments  路  Source: testing-library/jest-dom

  • @testing-library/jest-dom version: ^4.2.4
  • @testing-library/react version: ^9.4.0
  • node version: 10.13.0
  • npm (or yarn) version: 6.4.1 (npm)

Relevant code or config:

https://github.com/tom-sherman/jest-dom-nottobeindocument-bug

Specifically https://github.com/tom-sherman/jest-dom-nottobeindocument-bug/blob/master/src/App.test.js#L8

expect(linkElement).not.toBeInTheDocument();

What you did:

Added in a .not modifier before a .toBeInTheDocument call.

What happened:

Test failed when it should have passed

Reproduction:

Run npm t in this repo https://github.com/tom-sherman/jest-dom-nottobeindocument-bug

Problem description:

I should be able to use .not modifiers on all jest-dom matchers.

Suggested solution:

Most helpful comment

@tom-sherman - You may want to use queryByText instead of getByText. This way you don't need to check for toThrow.

All 2 comments

Oh I done goofed here...

I thought that the .not.toBeInTheDocument wasn't working correctly but in actual fact it was getByText thowing an error which is expected behaviour.

I can rewrite the test (https://github.com/tom-sherman/jest-dom-nottobeindocument-bug/commit/eca61346d83bec2a1d0212b916bc9a8852dcf6e9) to assert that this call throws.

@tom-sherman - You may want to use queryByText instead of getByText. This way you don't need to check for toThrow.

Was this page helpful?
0 / 5 - 0 ratings