When using testing-library with Vue it is easy to check if an element is not present in the DOM.
For example when v-if for the element is false:
element = screen.queryByTestId("my-element")
expect(element).toBeNull()
However when using v-show the element isn't removed from the DOM, it is toggled via an inline style of display: none, so the above won't work.
I can do something like this:
element = screen.queryByTestId("my-element")
expect(element.style.display).toEqual("none")
But this feels somewhat brittle. Is there a better way to handle this?
From
@testing-library/vue's Quickstart:The matchers contain .toBeVisible(), described as:
Note:
jest-domdoesn't care what framework you use. It evaluates HTML & CSS values, regardless of what directives generate those values. HTML and CSS output is what the user's browser receives.Side-note: since you mentioned it, checking if an element is in DOM should be done with
expect(queryBy*(*))[.not].toBeInTheDocument(). In the latest version of@testing-library, which contains very helpful linting rules, the recommendation is to usegetBy*queries for asserting presence in DOM andqueryBy*for asserting absence.