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

waitFor with getAllBy query not equivalent to findAllBy #1225

Open
Clarity-89 opened this issue Apr 5, 2023 · 0 comments
Open

waitFor with getAllBy query not equivalent to findAllBy #1225

Clarity-89 opened this issue Apr 5, 2023 · 0 comments

Comments

@Clarity-89
Copy link

  • @testing-library/dom version: 9.2.0
  • Testing Framework and version:
    • "@testing-library/react": "14.0.0",
    • Jest: "29.5.0"
  • DOM Environment:

Also using React 18.

Relevant code or config:

import { act, render, screen, waitFor } from "@testing-library/react";
import { TestComponent } from "./App";

const mockDataProvider = {
  start: jest.fn().mockImplementation(() => Promise.resolve()),
  getKeys: () => ["key1", "key2"],
};
const props = {
  dataProvider: mockDataProvider,
  updateFilter: jest.fn(),
  item: {
    entry: "Sample entry",
    attributes: {
      key1: "value1",
      key3: "value3",
    },
  },
};

describe("Test", () => {
  it("Passing test", async () => {
    jest.useFakeTimers();
    render(<TestComponent {...props} />);
    await waitFor(() => {
      expect(props.dataProvider.start).toHaveBeenCalled();
      expect(screen.getAllByRole("alert")).toHaveLength(2);
    });
    act(() => {
      jest.runAllTimers();
    });
    expect(props.updateFilter).toHaveBeenCalled();
    jest.useRealTimers();
  });

  it("Failing test", async () => {
    jest.useFakeTimers();
    render(<TestComponent {...props} />);

    await waitFor(() => {
      expect(props.dataProvider.start).toHaveBeenCalled();
    });
    expect(await screen.findAllByRole("alert")).toHaveLength(2);
    act(() => {
      jest.runAllTimers();
    });
    expect(props.updateFilter).toHaveBeenCalled();
    jest.useRealTimers();
  });
});

What you did:

What happened:

The first test succeeds while the seconds test fails.

Reproduction:

Codesandbox

Problem description:

Not sure if this is an actual bug, but I can't understand why the combination of awaitFor + getAllByRole passes the test but using findAllByRole doesn't. Aren't findBy* queries just a wrapper for awaitFor + getBy*? Also the second test doesn't really need the mock timers, but I've included those to make the tests more comparable (it doesn't change the test result).

Suggested solution:

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

1 participant