我尝试运行测试时出现错误

时间:2017-10-23 22:43:22

标签: javascript unit-testing react-native

这是控制台中的错误:Cannot find module 'expo' from 'setup.js'

以下是我的代码:

component.test.js

import React from 'react';
import MyComponent from '../components/Component';
import renderer from 'react-test-renderer';

test('create component correctly', () =>{
   const tree = renderer.create(
       <MyComponent/>
   ).toJSON();
   expect(tree).toMatchSnapshot();
});

Component.js

import React, { Component } from 'react';
import {Text, View } from 'react-native';

export default class MyComponent extends React.Component{
   render(){
       return(
          <View style={{flex: 1}}>
              <View style={styles.cameraContainer}>
                  <Text> Test Component </Text>
              </View>
          </View>
      );
   }
}

当我尝试使用npm测试时出现我之前提到的错误,我已经尝试卸载并重新安装jest并且错误一直显示。

1 个答案:

答案 0 :(得分:0)

假设您正在使用create-react-native-app,看起来这是来自&#34; jest-expo&#34;在jest配置中预设。

将其替换为react-native Jest预设,现在内置于Jest:

<强> jest.config.js:

module.exports = { preset: 'react-native' };

package.json

"jest": {
    "preset": "react-native"
  }