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.

[jest] Still throwing "not wrapped in act(...)" errors

See original GitHub issue

Follow up on https://github.com/pmndrs/zustand/issues/272#issuecomment-809660791.

After updating my test runner to jest-circus and wrapping each resetFn in act(...) I thought I got rid of this error, but it came up again after another state update that is done inside a useEffect on mount. This one reads a json file from the filesystem and stores it in a zustand store:

    refreshGallery: async () => {
      const galleryData = await api.readGallery();
      set((draft) => {
        draft.gallery = galleryData;
      });
    },

Which basically does the following (Electron app):

  ipcHandle('readGallery', async () => {
    const dbPath = path.join(app.getPath('userData'), 'media', 'database.json');
    const db = await fs.readFile(dbPath, 'utf-8');
    const data = JSON.parse(db);
    return data;
  });

The (newly) failing test is

test('sidebar exists', () => {
  render(<App />);
  expect(screen.getByTitle('nav.camera')).toBeInTheDocument();
});

I’ve managed to fix the wrapped in act(...) error by basically wrapping the entire test in async:

test('sidebar exists', async () => {
  await act(async () => {
    render(<App />);
  });
  expect(screen.getByTitle('nav.camera')).toBeInTheDocument();
});

Could this be a zustand issue, or is it something jest-related?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
italodeandracommented, May 21, 2021

I had the same problem with valtio. I also had the same process as you, I thought jest-circus had fixed it (https://github.com/pmndrs/zustand/issues/272#issuecomment-839943504), but if you have an effect that updates the store, then the problem will happen again.

The explanation is that the test ends before valtio/zustand end doing their thing to the proxy/store, so React triggers this warning, and I couldn’t find any way to make it wait for the change.

1reaction
squarismcommented, Apr 29, 2021

@ivancuric I watched the videos here btw: https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning There are lots of tips and examples there. And lots of explanation.

Adding

const promise = Promise.resolve()
await act(() => promise)

To the end of my test fixed this for me but I recommend watching those videos. Extremely helpful to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Testing Library and the “not wrapped in act” Errors
Case 4: Formik Updates​​ It goes like this: test simulates events to change values in form inputs, e.g. changing value in a text...
Read more >
reactjs - How to solve the "update was not wrapped in act() ...
If your code changes so that, for instance, after the list testId is found, the asserts run, then another useEffect is triggered which...
Read more >
Fix the "not wrapped in act(...)" warning
1. When using jest.useFakeTimers() ... The act warning here is happening because of this line: // ... let current = true function tick()...
Read more >
"test was not wrapped in act (...)" error - clear explanation?
In principle, it is about the fact that there was a race between the component and the logic in the test, in the...
Read more >
Fix the "not wrapped in act(...)" warning
[3:18] Then at the end of our test, we'll await that promise. We can make this async. Then we can await the promise....
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