Closed
Description
@testing-library/jest-dom
version: 5.11.9 (latest)node
version: n/anpm
(oryarn
) 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