react-native-testing-library
react-native-testing-library copied to clipboard
Unexpected 'act(async () => ...) without await' warning
Unexpected act(async () => ...) without await warning
In order to test this component:
const QuestionWidget = () => {
const { fireEvent } = useContext(AppContext);
const [question, setQuestion] = useState();
useEffect(() => {
retrieveQuestion();
}, []);
const retrieveQuestion = async () => {
try {
const question = await getQuestion();
setQuestion(question);
} catch (e) {
console.warn('Cannot retrieve original question', e);
}
};
const sendResponse = async answer => {
try {
await postAnswer(answer);
fireEvent('onAnswer', {});
} catch (e) {
console.warn('Cannot post the answer', e);
}
};
const onPress = answer => {
sendResponse(answer);
};
return (
<View>
{
question?.question.answers.map((answer, index) => (
<TouchableWithoutFeedback
key={index}
testID={answer}
onPress={onPress.bind(null, answer)}
>
<View>{answer}</View>
</TouchableWithoutFeedback>
))
}
</View>
);
};
I made this simple test using native-testing-library (7.1.0)
describe('<QuestionWidget />', () => {
beforeEach(() => {
nock.disableNetConnect();
});
it('should answer by clicking an answer', async () => {
nock(myUrl)
.get('/question')
.reply(200, {
question: {
answers: ['answer0', 'answer1'],
},
});
nock(myUrl)
.post('/question/answer')
.reply(200, { success: true });
const onAnswer = jest.fn();
const { findByTestId } = render(
<AppContext events={{ onanswer: onAnswer }} >
<Text>Test text</Text>
<QuestionWidget/>
</AppContext>
);
const answer = await findByTestId('answer0');
fireEvent.press(answer);
await waitFor(() => expect(onAnswer.mock.calls.length).toBe(1));
});
afterEach(() => {
nock.abortPendingRequests();
nock.cleanAll();
});
});
I've end up with a successfull test, but also with this warning message :
Warning: You called act(async () => ...) without await. This could lead to unexpected testing behaviour, interleaving multiple act calls and mixing their scopes. You should - await act(async () => ...);
I don't really get it, and if someone could explain it to me it would be very helpfull. Thanks for your help !
EDIT: I saw that an issue for this console warning was already made. This is my bad !
Closing as this is a duplicate.