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

Clean up document after test is finished #50

Closed
camdub opened this issue Feb 4, 2019 · 8 comments
Closed

Clean up document after test is finished #50

camdub opened this issue Feb 4, 2019 · 8 comments

Comments

@camdub
Copy link
Contributor

camdub commented Feb 4, 2019

Since this matcher appends the rendered content to the document (i.e., document.body.innerHTML = html), I noticed that other tests that render to the document via react-testing-librarycause there to be two instances of the component in the tree.

To avoid this, I added the following:

afterEach(() => {
  document.body.innerHTML = ''
})

However, it seems like this would best be handled by jest-axe itself. I can create a PR if needed.

@NickColley
Copy link
Owner

Hey @camdub,

That sounds sensible to me, I wonder if we could cleanup before we resolve the promise here:

https://github.com/nickcolley/jest-axe/blob/master/index.js#L40

Would love a pull request and I can publish this for you. 👍

@camdub
Copy link
Contributor Author

camdub commented Feb 4, 2019

@NickColley that was my thought too on the best way to do the cleanup.

I'll look to make a PR.

@thomasheyenbrock
Copy link
Contributor

I stumbled upon the same problem recently! Good to see I'm not the only one. 😅

Clearing the content of document.body is a step in the right direction. But I think now you can't do any more assertions after running axe.

import React from "react";
import { axe, toHaveNoViolations } from "jest-axe";
import { render } from "react-testing-library";

expect.extend(toHaveNoViolations);

it("should be able to make multiple assertion", async () => {
  const { container, getByText } = render(<h1>Hello world!</h1>);
  expect(await axe(container.innerHTML)).toHaveNoViolations();
  // I think this will fail, since document.body is empty now :(
  expect(getByText("Hello world!")).toBeInTheDocument();
})

@NickColley
Copy link
Owner

@thomasheyenbrock hmm, I've not published anything yet so we've got time to get this right.

Do you have an suggestions on an alternative way to solve this problem?

@thomasheyenbrock
Copy link
Contributor

I proposed a solution in #52

@NickColley
Copy link
Owner

Published as version 3.1.1 thanks again.

@kentcdodds
Copy link
Contributor

Note, if you're using react-testing-library you should be using cleanup.

@NickColley
Copy link
Owner

@kentcdodds if you want to feel free to open a PR to update the readme with that advice

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

No branches or pull requests

4 participants