【问题标题】:Error on jest when I try to run the test当我尝试运行测试时开玩笑的错误
【发布时间】:2017-10-23 22:43:21
【问题描述】:

这是控制台中的错误: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 test 时,出现了我之前提到的错误,我已经尝试卸载并重新安装 jest 并且错误一直显示。

【问题讨论】:

  • 弹出后我遇到了同样的错误。在我的代码中没有使用任何 expo API。你弹射了吗?

标签: javascript unit-testing react-native


【解决方案1】:

假设您使用的是create-react-native-app,看起来这是来自 jest 配置中的“jest-expo”预设。

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

jest.config.js:

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

或者在package.json中:

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

【讨论】:

  • 我这样做了,并且我使用npm prune 清除了依赖项,然后我再次安装了缺少依赖项的依赖项,它工作正常。谢谢这个帮助我!
猜你喜欢
  • 1970-01-01
  • 2020-12-21
  • 2020-09-01
  • 1970-01-01
  • 2018-08-05
  • 2020-12-02
  • 1970-01-01
  • 2022-07-29
  • 1970-01-01
相关资源
最近更新 更多