类型错误:无法读取未定义的属性“SHORT”

kom*_*alV 3 unit-testing jestjs react-native enzyme react-native-ios

我正在使用笑话和酶为 React Native 应用程序编写单元测试。当我尝试运行测试用例时,出现以下错误:

\n\n
\n

\xe2\x97\x8f 测试套件运行失败

\n\n

类型错误:无法读取未定义的属性“SHORT”

\n\n

在对象。(node_modules/react-native-simple-toast/index.js:7:23)\n 在对象处。(src/client/components/Catalogue/events.js:10:29)\n 在对象处。(测试/events-test.js:5:13)\n 在 Generator.next ()\n 在 new Promise ()\n 在 Generator.next ()\n 在

\n
\n\n

该错误是由于从react-native-simple-toast导入Toast造成的。如果没有这个导入,测试运行正常,但我的组件中需要react-native-simple-toast,因此无法删除它。我以某种方式需要测试在我的组件中使用react-native-simple-toast运行:

\n\n
import React, { Component } from 'react'\nimport { Platform, ActivityIndicator, Linking, Dimensions, View, Text, \nTouchableOpacity, TextInput, StyleSheet, Keyboard, Image, \nKeyboardAvoidingView } from 'react-native'\nimport { Container, Header, Card, CardItem, Content, Left, Right, Body, \nTitle, Icon, Button } from 'native-base';\nimport IconIOSback from 'react-native-vector-icons/Ionicons';\nimport IconMenu from 'react-native-vector-icons/MaterialIcons';\nimport axios from 'axios';\nimport SERVER_URL from '../../config';\nimport { Col, Row, Grid } from 'react-native-easy-grid';\nimport Toast from 'react-native-simple-toast';   //this causes the error\n
Run Code Online (Sandbox Code Playgroud)\n\n

请帮我解决这个问题。

\n

小智 7

解决这个问题的方法是模拟该属性SHORT。创建一个测试设置文件,例如setup.js并包含以下模拟

jest.mock('react-native-simple-toast', () => ({
  SHORT: jest.fn(),
}));
Run Code Online (Sandbox Code Playgroud)

确保将此文件添加到您的配置中package.jsonjest

"jest": 
.....
"setupFiles": [
      "/setup.js" // location of your setup.js file
    ]
Run Code Online (Sandbox Code Playgroud)