[英]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.