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

Stale DOM with fake timers in jest 27.x #45

Closed
marvinhagemeister opened this issue Nov 1, 2021 · 1 comment
Closed

Stale DOM with fake timers in jest 27.x #45

marvinhagemeister opened this issue Nov 1, 2021 · 1 comment

Comments

@marvinhagemeister
Copy link

  • preact-testing-library version: 2.0.1
  • preact version: 10.5.15
  • node version: 16.11.1
  • npm (or yarn) version: 8.0.0

Relevant code or config

What you did:

What happened:

Reproduction repository:

const { h } = require("preact"); /** @jsx h */
const { useState, useEffect } = require("preact/hooks");
const { render, waitFor, screen } = require("@testing-library/preact");

jest.useFakeTimers();

const apiCall = () => {
  return new Promise((resolve, reject) => {
    resolve();
  });
};

function Comp() {
  const [val, setVal] = useState("original value");

  useEffect(() => {
    const interval = setInterval(() => {
      apiCall().then(() => {
        setVal("timeout value");
      });
    }, 300); // <- 300ms delay
    console.log("call eff");
    apiCall().then(() => {
      console.log("resolve eff");
      setVal("effect value");
    });
    return () => {
      clearInterval(interval);
    };
  }, []);

  console.log("render", val);

  return <div>{val}</div>;
}

it("fails", async () => {
  const wrapper = render(<Comp />);
  const { container } = wrapper;

  expect(container.textContent).toEqual("original value");

  await waitFor(() => {
    console.log("check", container.textContent);
    expect(container.textContent).toEqual("effect value");
  });
  jest.advanceTimersByTime(1000);

  await waitFor(() => {
    expect(container.textContent).toEqual("timeout value");
  });
});

Problem description:

Due to the fake timers usage the first waitFor call times out. The DOM value is stale and never updated, despite the component correctly being re-rendered.

Suggested solution:

This issue is resolved by forcefully updating the dependency on @testing-library/dom to the latest version 8.10.1.

@JoviDeCroock
Copy link
Member

Bumped as of PTL 3.0.0

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

2 participants