Replies: 3 comments 8 replies
-
Will we still need to mock the
|
Beta Was this translation helpful? Give feedback.
-
One thing that's unclear to me is how we're supposed to test timer based updates (e.g. In React 17 all we had to do was wrap some polling function in await ReactDOMTestUtils.act(async () => {
await new Promise((resolve) => {
setInterval(async () => {
if (
// That's the condition we wait for
document.querySelector("main").getAttribute("aria-busy") !== "true"
) {
resolve();
}
}, 50);
});
}); The above code is abstracted away in In concurrent React the above code times out. With fake timers there's no change as far as I can tell since we can wrap ReactDOMTestUtils.act(() => {
// hardcode the time it take to resolve the mocked fetch
jest.advanceTimersByTime(100);
}); Full repro: https://github.com/eps1lon/react-concurrent-testing-timers |
Beta Was this translation helpful? Give feedback.
-
We have a single (I think) test that executes Emotion (and by extension also React) in production mode: The test is using RTL and it was always resulting in an act-related error: What would be the recommended approach to refactor this? The RTL API is OK and it's cool to just reuse the library here, OTOH it is designed around act, right? Should this test just switch to the barebones DOM Testing Library? cc @eps1lon |
Beta Was this translation helpful? Give feedback.
-
(spawned from #21 (reply in thread))
There's a lot of planned work that needs to be done before release. What's implemented today isn't how we want it to work in the future.
The current implementation of act is more convoluted than it could be because we originally thought we needed to support act in production mode. But we also didn't want to add act logic to the production React DOM bundle. So we had to implement act using clever user space tricks that don't work exactly as we'd like.
Instead, we decided that act doesn't need to exist in production mode. Production mode is only used for testing in e2e environments, where act isn't useful. (In an e2e environment, you perform an interaction and wait for the DOM to update; no special framework integration required.) act is really designed for integration and unit tests, which in practice everyone runs in development only.
Because act will only exist in development mode, we can add additional logic to the development build of React DOM. What it will do is post React tasks on a special React queue, instead of posting them to the browser like in a real app. Then act can flush that queue directly — and synchronously — without any clever hacks.
The upshot is that the sync version of act will work as you describe. No need for await null to flush promise microtasks because we won't have scheduled a microtask at all. Though it's still useful for cases where you want to simulate an async event handler.
cc: @threepointone with whom we made this decision when he was on our team (and who is the creator of the API) 🙂
Update
I've posted a related proposal here: #102
Beta Was this translation helpful? Give feedback.
All reactions