Skip to content

Re-render renders component from scratch when used in separate tests #573

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

Closed
anzorb opened this issue Jan 21, 2020 · 2 comments
Closed

Re-render renders component from scratch when used in separate tests #573

anzorb opened this issue Jan 21, 2020 · 2 comments

Comments

@anzorb
Copy link

anzorb commented Jan 21, 2020

It appears that using re-render inside separate tests does not update props and instead re-renders the component from scratch. Is this by design?

See https://codesandbox.io/s/testing-libraryreact-re-render-issue-o1osd for a complete example

export const Bla = ({ hooo }) => {
  const id = useRef(0);

  useEffect(() => {
    id.current++;
    console.log("id:", id.current);
  }, [hooo]);

  return <div>{hooo}</div>;
};
describe("bla", () => {
  let instance;
  beforeAll(() => {
    instance = render(<Bla hooo={3} />);
  });

  it("works when in one test", () => {
    instance.rerender(<Bla hooo={4} />);
    instance.rerender(<Bla hooo={5} />);
    instance.rerender(<Bla hooo={6} />);
    // output id: 1, 2, 3, 4
    expect(true).toBe(true);
  });

  it("doesn't work 1", () => {
    instance.rerender(<Bla hooo={7} />);
    // output id: 1
    expect(true).toBe(true);
  });

  it("doesn't work 2", () => {
    instance.rerender(<Bla hooo={8} />);
    // output id: 1
    expect(true).toBe(true);
  });
});
@eps1lon
Copy link
Member

eps1lon commented Jan 21, 2020

Thank you for opening this issue and providing a reproduction.

This is expected. We automatically clean up between each test as of #430. It explains how you can disabled this behavior though we do not recommend it.

@eps1lon eps1lon closed this as completed Jan 21, 2020
@kentcdodds
Copy link
Member

Here's more info on why: https://kentcdodds.com/blog/avoid-nesting-when-youre-testing

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

3 participants