Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toContainHTML does not handle self-closing tags #347

Closed
just-boris opened this issue Mar 10, 2021 · 2 comments · Fixed by #349
Closed

toContainHTML does not handle self-closing tags #347

just-boris opened this issue Mar 10, 2021 · 2 comments · Fixed by #349
Labels

Comments

@just-boris
Copy link
Contributor

just-boris commented Mar 10, 2021

  • @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

@gnapse
Copy link
Member

gnapse commented Mar 11, 2021

Thanks for the reports. What you suggest seems a reasonable approach. Can you contribute the change yourself?

just-boris added a commit to just-boris/jest-dom that referenced this issue Mar 18, 2021
just-boris added a commit to just-boris/jest-dom that referenced this issue Mar 19, 2021
just-boris added a commit to just-boris/jest-dom that referenced this issue Mar 23, 2021
gnapse pushed a commit that referenced this issue Mar 25, 2021
@github-actions
Copy link

🎉 This issue has been resolved in version 5.11.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants