Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

An update to Test inside a test was not wrapped in act. With v5.0.1 #84

Closed
vladwhd opened this issue Nov 29, 2019 · 4 comments
Closed

Comments

@vladwhd
Copy link

vladwhd commented Nov 29, 2019

Issue description

With the latest version of the library and with the latest RN I get the following error in the console:

Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Warning: An update to Test inside a test was not wrapped in act(...).

When testing, code that causes React state updates should be wrapped into act(...):

There is more output in the console, but this is essential I think. Not sure if it is really related to #25

Also, test is marked as successful.

With @testing-library/react an equivalent test in a web app project causes no problems at all.

How to reproduce

npx react-native init TestApp
npm i --save-dev @testing-library/react-native

Create a Test.test.js inside of __tests__ with following content:

import React, {useEffect, useState} from 'react';
import {Text, View} from 'react-native';
import {render} from '@testing-library/react-native';

const Test = () => {
    const [v, setV] = useState(1);
    useEffect(()=>{
        setTimeout(() => {
            setV(2);
        }, 0)
    }, []);
    return <View><Text>{v}</Text></View>
};
test('Test', () => {
    render(<Test />);
});

Then npm run test

Versions

  • react-native or expo: react-native
  • native-testing-library version: 5.0.1
  • jest-preset: @testing-library/react-native
  • react-native version: 0.61.3
  • node version: v13.2.0
@vladwhd vladwhd changed the title An update to ... inside a test was not wrapped in act. With v5.0.1 An update to Test inside a test was not wrapped in act. With v5.0.1 Nov 29, 2019
@bcarroll22
Copy link
Collaborator

Hi thanks for the issue. I don't think it's related to #25, but it is probably related to testing-library/react-testing-library#535. We should stay subscribed to that issue here.

Related, what version of React do you have installed? The reason I ask is that react native 0.61.3 requires react: 16.9.0 (explicitly, no other version is supported) so this shouldn't be related to a version bump of testing library since act comes from React. Are you by chance using a later version of React and React Test Renderer?

@vladwhd
Copy link
Author

vladwhd commented Nov 29, 2019

I'm using 16.9.0 of React. Just tried to update to react@latest and got 16.12.0. Looks like it didn't change anything for this error.
Feels like it's useEffect that is not handled properly. Because I have no problems with example (https://testing-library.com/docs/native-testing-library/example-intro). I'll keep investigating on Monday :)

@bcarroll22
Copy link
Collaborator

This should fix it for you. The issue is not cleaning up the timeout.

const Test = () => {
  const [v, setV] = React.useState(1);
  React.useEffect(()=>{
    const timeout = setTimeout(() => setV(2), 0);
    return () => clearTimeout(timeout);
  }, []);
  return <View><Text>{v}</Text></View>
};
test('Test', () => {
  render(<Test />);
});

@vladwhd
Copy link
Author

vladwhd commented Dec 2, 2019

Yes, it was this indeed, thank you very much!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants