question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Intermittent test failures when upgrading from 3.3 to 3.4.16

See original GitHub issue

I recently updated a project from version 3.3 to 3.4.16 and I’m now seeing intermittent errors in my tests. The tests are using React and @testing-library/react with jest. The main issue is that tests occasionally get stuck in the “Loading” state. In the documentation, https://www.apollographql.com/docs/react/development-testing/testing/#testing-the-success-state, states that all I should need to do is await a 0ms timeout, but that’s already being done. Is this a bug with @apollo/client or am I doing something wrong here?

An example of a test that is failing randomly.

test("renders control view when data exists", async () => {
  await act(async () => {
    renderControlViewForMockId(CONTROL_WITH_TESTS_AND_RELATED_CTRLS);
    await new Promise((resolve) => setTimeout(resolve, 0));
  });

  const text = screen.getAllByText("Control 1");
  expect(text.length).toBe(2);
  expect(text[0]).toBeInTheDocument();
  expect(text[1]).toBeInTheDocument();
});

The render method.

const renderControlViewForMockId = (id: string) => {
  render(
    <MockedProvider mocks={[...queryMocks, ...eventMocks]} addTypename={false}>
      <MemoryRouter initialEntries={[`/testUrl/${id}`]}>
        <Route path="/base/:baseId/sub/:subId">
          <ModalProvider>
            <ModalWrapper>
              <ControlContainer />
            </ModalWrapper>
          </ModalProvider>
        </Route>
      </MemoryRouter>
    </MockedProvider>
  );
};

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bignimbuscommented, Dec 5, 2022

Hi all, I’ll close this issue for now since it appears to have been resolved in newer releases. Please let us know if you need more support here 🙏🏻

1reaction
brainkimcommented, Oct 12, 2021

There is a findAllByText() method which seems to wrap getAllBy() call in an act so that it is awaitable (https://testing-library.com/docs/dom-testing-library/cheatsheet/#queries). I think that might help your use-case?

seems like it is covering up a symptom rather than the cause. This issue only happens when updating our apollo client version. I don’t know how to reliably reproduce this since the errors are not consistent and it seems to be an issue with the timing during the test.

I appreciate the concern, but I believe your issues are with React, not Apollo Client. We don’t consider the exact timings of the hooks to be part of the public interface, and APIs like useEffect() and useState() are not guaranteed to run synchronously, even if that’s how they work today. The hooks are being heavily refactored, and the timings will change between minor versions. The fact that react-testing-library has to implement all these polling methods is more or less an admission of this lack of precision on React’s part.

I would try awaiting a screen.findAllByText() call and seeing if that helps with the flake!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix Flaky Tests - Semaphore CI
Step 1 – Commit to fixing the problem right away! ... A test that intermittently fails for no apparent reason — or works...
Read more >
Release Notes - ZooKeeper - Version 3.4.14
ZOOKEEPER-3262 - Update dependencies flagged by OWASP report ... ZOOKEEPER-2712 - MiniKdc test case intermittently failing due to principal not found in ...
Read more >
Introduction to Testing — Phoenix v1.6.15 - HexDocs
If we notice that a specific random seed triggers our intermittent failure, we can re-run the tests with that same seed to reliably...
Read more >
Release Notes · CloudOps for AWS
3.4.16 ` ... SUP-408 Fixed commerce pipeline intermittent failures when ... CLOUD-1801 - Update AWS Lambda functions to use Node.js 12.
Read more >
Release Notes for MongoDB 3.4
SERVER-37058 : Update with numeric field names inside an array can cause ... sync retry logic to be more resilient to intermittent failures...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found