测试react-native组件

时间:2018-06-06 14:15:02

标签: react-native jestjs

我正在使用git的react-native源来实现项目依赖性, 我将RN源放在外部目录(而不是node_modules)中,并将其添加到rn-cli.js中的extraNodeModules,因此RN打包器可以正确找到所有源。 但是在运行jest时,它找不到任何RN内置组件。

测试文件:

import { View } from 'react-native';
import React from 'react';
import mockCamera from '../__mocks__/camera';
import renderer from 'react-test-renderer';
jest.mock('react-native-camera', () => mockCamera);

var TableIDInputer = props => <View />;
test('renders correctly', () => {
    const component = renderer.create(<TableIDInputer />);
    let tree = component.toJSON();

    expect(tree.type).not.toBe('b');
    expect(tree).toMatchSnapshot();
});

测试结果:

  

●正确渲染

Cannot find module 'View' from 'react-native-implementation.js'

  146 |   },
  147 |   get View() {
> 148 |     return require('View');
  149 |   },
  150 |   get ViewPagerAndroid() {
  151 |     return require('ViewPagerAndroid');

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:169:17)
  at Object.get View [as View] (../../libs/react-native/Libraries/react-native/react-native-implementation.js:148:12)
  at TableIDInputer (__tests__/test-component.js:7:31)
  at mountIndeterminateComponent (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:4137:15)
  at beginWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:4541:16)

因为View组件是由haste module提供的,之前由@providesModule实现,但今天它似乎被新机制所取代,所以我怀疑它是不是导致测试失败??

env info: 开玩笑:^ 22.4.3 react-native:2018-5-31 6e359c4589b0627de59332ce56fe28804425a609

0 个答案:

没有答案