0

I have a test:

import csvDownload from "../../../utils/methods/csvDownload";

jest.mock("../../../utils/methods/csvDownload");

describe("Test Dashboard Page", () => {
  it("Test can dowload csv", async () => {
    csvDownload.mockResolvedValue(null);
    renderWithProviders(<DashboardPage />);
    await waitFor(() => {
      const downloadCsv = screen.getByText("97");
      fireEvent.click(downloadCsv);
      expect(csvDownload).toHaveBeenCalledTimes(1);
    });
  });
});

But I have a bigger amount of times called (~ 70).

Cannot reset that count.

I have seen this question: How to reset Jest mock functions calls count before every test

so adding the afterEach:

import csvDownload from "../../../utils/methods/csvDownload";

jest.mock("../../../utils/methods/csvDownload");

afterEach(() => {
  jest.clearAllMocks();
});

describe("Test Dashboard Page", () => {
  it("Test can dowload csv", async () => {
    csvDownload.mockResolvedValue(null);
    renderWithProviders(<DashboardPage />);
    await waitFor(() => {
      const downloadCsv = screen.getByText("97");
      fireEvent.click(downloadCsv);
      expect(csvDownload).toHaveBeenCalledTimes(1);
    });
  });
});

(Tried also csvDownload.resetMock() without luck)

sineverba
  • 5,059
  • 7
  • 39
  • 84
  • There's a chance you may be putting a little too much work into the "waitFor" function. What is the mock value you're seeing when you run "Test can download csv"? – mjwils Feb 17 '23 at 10:52
  • 1
    At the end I solved moving the method under test in another file and making a ligh refactor of code. For real, when a method makes too many things, it is difficult to test it. – sineverba Feb 19 '23 at 09:01

0 Answers0