[jest] Still throwing "not wrapped in act(...)" errors
See original GitHub issueFollow 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:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I had the same problem with
valtio
. I also had the same process as you, I thoughtjest-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.
@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
To the end of my test fixed this for me but I recommend watching those videos. Extremely helpful to me.