Skip to content

toContainHTML does not handle self-closing tags #347

Closed
@just-boris

Description

@just-boris
  • @testing-library/jest-dom version: 5.11.9 (latest)
  • node version: n/a
  • npm (or yarn) version: n/a

Relevant code or config:

import "@testing-library/jest-dom/extend-expect";
import React from "react";
import { render } from "@testing-library/react";

test("demo test", () => {
  const { container } = render(<div className="empty" />);
  // this assertion passes
  expect(container).toContainHTML('<div class="empty"></div>');
  // this one fails, even though it references the same structure
  // expect(container).toContainHTML('<div class="empty" />');  
});

Reproduction:

https://codesandbox.io/s/contains-html-assertion-demo-7nd5n?file=/src/__tests__/hello.js:0-343

Suggested solution:

toContainHTML matcher treats expected content as a string. I figured that this normalization fixes the matching:

function normalize(content) {
   const div = document.createElement('div');
   div.innerHTML = content;
   return div.innerHTML;
}

expect(container).toContainHTML(normalize('<div class="empty" />'));  // passes  

Perhaps the matcher inside could apply this normalization

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions