57

I'm testing a component where if ItemLength = 1, render returns null.

const { container, debug } = render(<MyComp ItemLength={1} />);

When I call debug() in my test, it shows a <div />. How do I check that the component is returning an empty div in my test?

1
  • getByText(container, (content, element) => element.tagName.toLowerCase() === 'div') try using getByText to match element TagName Commented May 23, 2019 at 2:46

7 Answers 7

70

Update

Use toBeEmptyDOMElement since toBeEmtpy has been deprecated.


You can use jest-dom's toBeEmpty:

const { container } = render(<MyComp ItemLength={1} />)
expect(container.firstChild).toBeEmpty()
Sign up to request clarification or add additional context in comments.

4 Comments

It appears this toBeEmpty has been deprecated since jest-dom 5.9.0.
As @TheTFo states above, this solution appears not to work as of my writing this in late 2020. Karolis Grinkevičius's answer below works for me.
use toBeEmptyDOMElement as toBeEmpty has been deprecated
toBeNull() or toBeFalsy() is what I would use, now that toBeEmpty is deprecated
47

The following should work as well without extending jest's expect:

const { container } = render(<MyComp ItemLength={1} />)
expect(container.firstChild).toBeNull();

Update: the new way in 2020

import { screen } from '@testing-library/react';

...

render(<MyComp ItemLength={1} />);

const child = screen.queryByTestId('data-testid-attribute-value');

expect(child).not.toBeInTheDocument();

4 Comments

the new way requires you to have an ID but if ur testing something doesn't exist why would there be a test id?
@Batman it doesn't necessarily have to be data-testid attribute. Ideally you should be querying an element by accessibility related attributes, e.g. screen.queryByRole, screen.queryByText or other query documented here.
Yeah but what can you do if there is no role/text/anything else to query by?
@akerr In case you don't have anything specific to query for, you can always rely on using document.querySelector, e.g. document.querySelector('.yourClassName').
14

.toHaveLength(0) should also work without jest-dom extension

const wrapper = render(<MyComp ItemLength={1}/>);
expect(wrapper.container.innerHTML).toHaveLength(0);

Comments

11

toBeEmpty - throw warning, you must use toBeEmptyDOMElement instead

    const pageObject = cretePage(<UserResources />, appState);

    expect(pageObject.noContent).toBeInTheDocument();
    expect(pageObject.results).toBeEmptyDOMElement();

Comments

5

To extend off the previous answers; the following works and is probably a bit cleaner with no dependence on the data-testid or screen to check for roles.

import { render } from "@testing-library/react";

// Components
import { YourComponent } from "<Path>/YourComponent";

describe("YourComponent", () => {
  it("component should be an empty element", () => {
    const { container } = render(<YourComponent />);

    expect(container).toBeEmptyDOMElement();
  });

  it("component should not be an empty element", () => {
    const { container } = render(<YourComponent />);

    expect(container).not.toBeEmptyDOMElement();
  });
});

Comments

3

You can use js-dom's toBeEmptyDOMElement method. https://github.com/testing-library/jest-dom#tobeemptydomelement

Before you can use toBeEmptyDOMElement you will need to install jest-dom and set up jest. https://github.com/testing-library/jest-dom#usage

const { container } = render(<MyComp ItemLength={1} />)
expect(container.firstChild).toBeEmptyDOMElement()

Note: toBeEmpty method is being deprecated and its suggested to use toBeEmptyDOMElement

3 Comments

With this approach, I got TypeError: expect(...).toBeEmptyDOMElement is not a function
@Hiroki You need to install jest-dom before you can use it. Updating the answer to include that.yarn add --dev @testing-library/jest-dom
You may not have (or want) a dependency on @testing-library/jest-dom if you are a React Native developer. In this case, you can use toJSON, e.g. expect(toJSON()).toBeNull()
1

Since you are trying to test for empty div, one way you could try to test it is by matching node (another possible solution is number of nodes rendered)

getByText(container, (content, element) => element.tagName.toLowerCase() === 'div')

1 Comment

not sure how this would work. passing container in throws error since it expects a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.