According to the react unit testing documentation:
act()
To prepare a component for assertions, wrap the code rendering it and performing updates inside an act() call. This makes your test run closer to how React works in the browser.
But the test runs perfectly fine in both cases:
Without act()
it('Should return some text', () => {
render(<TestComponent />, container);
expect(container.textContent).toBe('some text');
});
With act()
it('Should return some text', () => {
act(() => {
render(<TestComponent />, container);
});
expect(container.textContent).toBe('some text');
})
The questions is: What exactly act() does, and when should someone use it?
act()
documentation? – Mettah