简体   繁体   中英

React-native test cases fail

I'm trying to write some test cases of a simple react-native app, but my test case always fails with an error which apparently cannot find UIManager.createView

Component

class LiveTab extends Component {
    constructor(props) {
        ...
    }
    render() {
        return (
            <ListView
                dataSource={ ... }
                renderRow={ ... }
                />
        );
    }
}

LiveTab.propTypes = { ... };

export default LiveTab;

Test case

...
import renderer from 'react-test-renderer';

it('LiveTab renders correctly', () => {
    const tree = renderer.create(
        <LiveTab ... />
    );
});

Error

FAIL tests /components/LiveTab.js (14.766s) ● LiveTab renders correctly

TypeError: UIManager.createView is not a function

at Constructor.mountComponent (node_modules/react-native/Libraries/Renderer/src/renderers/native/ReactNativeBase Component.js:201:11)

at Object.mountComponent (node_modules/react-test-renderer/lib/ReactReconciler.js:46:35) at ReactCompositeComponentWrapper.performInitialMount (node_modules/react-test-renderer/lib/ReactCompositeCompon ent.js:371:34) at ReactCompositeComponentWrapper.mountComponent (node_modules/react-test-renderer/lib/ReactCompositeComponent.js:258:21) at Object.mountComponent (node_modules/react-test-renderer/lib/ReactReconciler.js:46:35) at ReactTestComponent.mountChildren (node_modules/react-test-renderer/lib/ReactMultiChild.js:238:44) at ReactTestComponent.mountComponent (node_modules/react-test-renderer/lib/ReactTestRenderer.js:62:10) at Object.mountComponent (node_modules/react-test-renderer/lib/ReactReconciler.js:46:35) at ReactCompositeComponentWrapper.performInitialMount (node_modules/react-test-renderer/lib/ReactCompositeCompon ent.js:371:34) at ReactCompositeComponentWrapper.mountComponent (node_modules/react-test-renderer/lib/ReactCompositeComponent.js:258:21)

I am not able to figure out what the problem is in such a simple component.

I was getting the same error because one of the modules I was importing into my component was not getting transformed by babel, so it needed to be whitelisted in package.json

See example:

https://facebook.github.io/jest/docs/tutorial-react-native.html#transformignorepatterns-customization

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM