Open enzzoperez opened 2 years ago
Same issue here! The problem comes when using more than one awaited waitFor in the same it
Already read the act behavior in this PR https://github.com/callstack/react-native-testing-library/pull/969/files and tried all the suggestions of this issue https://github.com/callstack/react-native-testing-library/issues/379.
We can fix the problem using the solution proposed on https://stackoverflow.com/questions/64952449/react-native-testing-act-without-await/69201830#69201830 It seems that react-native jest preset is replacing global Promise implementation (2020 issue https://github.com/facebook/react-native/issues/29303) and this leads to the act-await warning.
The patch consists in add one previous preset and one post preset in order to save the original implementation and restore it after react-native preset is set. We do this and found that many tests started to fail (many), and in order to not debug each one, we change the patch to save the original implementation (in first preset) in global.nativePromise
and polyfilled in global.polyfilledPromise
(in last preset). Later, when we need to fix the Act Await warning, we swap the implementations only for a specific test suite calling fixActAwaitWarning
:
const fixActAwaitWarning = (): void => {
beforeAll(() => {
global.Promise = global.nativePromise;
});
afterAll(() => {
global.Promise = global.polyfilledPromise;
});
};
Thank you for your effort and investigation into this @tcank.
We can fix the problem using the solution proposed on https://stackoverflow.com/questions/64952449/react-native-testing-act-without-await/69201830#69201830 It seems that react-native jest preset is replacing global Promise implementation (2020 issue facebook/react-native#29303) and this leads to the act-await warning.
The patch consists in add one previous preset and one post preset in order to save the original implementation and restore it after react-native preset is set. We do this and found that many tests started to fail (many), and in order to not debug each one, we change the patch to save the original implementation (in first preset) in
global.nativePromise
and polyfilled inglobal.polyfilledPromise
(in last preset). Later, when we need to fix the Act Await warning, we swap the implementations only for a specific test suite callingfixActAwaitWarning
:const fixActAwaitWarning = (): void => { beforeAll(() => { global.Promise = global.nativePromise; }); afterAll(() => { global.Promise = global.polyfilledPromise; }); };
Do you mean to change the content of restore-promise.js like this:
global.polyfilledPromise = Promise;
I found it really works!
This is still happening. It only happens when I use more than one awaited waitFor in the same it
.
I came to this solution:
// https://github.com/testing-library/react-hooks-testing-library/issues/825
const asyncAct = (fn: () => Promise<unknown>) =>
new Promise((resolve) => {
act(fn).then(resolve);
});
Using it instead of act
avoid the warning.
Hi!, Im starting to test my hooks (in a POC stage), I have a simple test that works but I get the following warning that I dont know how can it solved :thinking:
this is my hook
and in my test file, Im trying to test that if the props change, the data will change too
Am I forgetting something in the test file?
Thanks!