2

I am working on test cases in jest but the problem i am facing is that before i updated the @testing-library/react "await waitFor" was working fine but after the update they are giving error (ex: not giving the expected output) but when I put removed "await waitFor" and used "setTimeout" the test started working fine. IDK it is happening .It is really confusing for me.

`it("toast displays client key exists message", async () => { const uniqueContraintError = { description: "Key for client name already exists" }; mockApiPostClientKey(400, uniqueContraintError);

    render(<AddClient onSave={(_): void => {}} clientCreateEndpoint="/api/clients/"
        clientKeyCreateEndpoint="/api/clients/keys/" />);

    fillInClientNameInput(clientName);
    clickAddButton();

    await waitFor(() => expect(toast.error).toHaveBeenCalledWith(uniqueContraintError));
});`

I removed await waitFor and added the setTimeout and the code is working fine and test case passed. `it("toast displays client key exists message", async () => { const uniqueContraintError = { description: "Key for client name already exists" }; mockApiPostClientKey(400, uniqueContraintError);

    render(<AddClient onSave={(_): void => {}} clientCreateEndpoint="/api/clients/"
        clientKeyCreateEndpoint="/api/clients/keys/" />);

    fillInClientNameInput(clientName);
    clickAddButton();

    setTimeout(() => expect(toast.error).toHaveBeenCalledWith(uniqueContraintError));
});`

0 Answers0