简体   繁体   English

开玩笑时出现错误,我尝试运行测试

[英]Error on jest when I try to run the test

This is the error in the console: Cannot find module 'expo' from 'setup.js' . 这是控制台中的错误: Cannot find module 'expo' from 'setup.js'

Below is my code: 下面是我的代码:

component.test.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 : 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>
      );
   }
}

When I try to use npm test the error that I mentioned before appears and I already try to uninstall and re-install jest and the error keeps showing. 当我尝试使用npm test时,出现了我之前提到的错误,并且我已经尝试卸载并重新安装jest,并且该错误不断显示。

Assuming you are using create-react-native-app , looks like this is coming from the "jest-expo" preset in the jest configuration. 假设您正在使用create-react-native-app ,看起来这是来自jest配置中预设的“ jest-expo”。

Replace it with the react-native Jest preset, which is now built in to Jest: 将其替换为react-native Jest预设,该预设现在内置在Jest中:

jest.config.js: jest.config.js:

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

Or in package.json : 或在package.json中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:当我尝试使用JEST测试方法时,dispatch不是函数 - TypeError: dispatch is not a function when I try to test a method using JEST 尝试运行测试套件时如何解决玩笑错误? - How to resolve jest error when attempting to run test suite? 当我尝试在Couchbase Node.js服务器上运行简单测试时,出现“参数错误”错误 - I get “Incorrect argument” error when i try to run a simple test on couchbase nodejs server 当我尝试测试运行我的 MySQL 数据库时收到此错误消息 - I get this error message when I try to test run my MySQL database 如何强制 mongodb 错误以开玩笑的方式测试 try and catch 条件 - How can I force a mongodb error to test with jest the try and catch condition 当我尝试运行mocha测试时,工作人员没有定义 - Worker not defined when I try to run mocha test 当我尝试测试我的重试工具 function 时,jest.advanceTimersByTime 不起作用 - jest.advanceTimersByTime doesn't work when I try to test my retry util function 为什么在使用 jest &#39;??=&#39; 进行测试时出现语法错误 - Why I got syntax error when doing test with jest '??=' 如何跳过开玩笑编译错误并运行测试? - How to skip jest compile error and run test? 运行玩笑测试时意外导入令牌 - Unexpected token import when run jest test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM