gpt4 book ai didi

unit-testing - 类型错误 : Cannot read property 'SHORT' of undefined

转载 作者:行者123 更新时间:2023-12-02 20:18:28 26 4
gpt4 key购买 nike

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

● Test suite failed to run

TypeError: Cannot read property 'SHORT' of undefined

at Object. (node_modules/react-native-simple-toast/index.js:7:23) at Object. (src/client/components/Catalogue/events.js:10:29) at Object. (tests/events-test.js:5:13) at Generator.next () at new Promise () at Generator.next () at

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

import React, { Component } from 'react'
import { Platform, ActivityIndicator, Linking, Dimensions, View, Text,
TouchableOpacity, TextInput, StyleSheet, Keyboard, Image,
KeyboardAvoidingView } from 'react-native'
import { Container, Header, Card, CardItem, Content, Left, Right, Body,
Title, Icon, Button } from 'native-base';
import IconIOSback from 'react-native-vector-icons/Ionicons';
import IconMenu from 'react-native-vector-icons/MaterialIcons';
import axios from 'axios';
import SERVER_URL from '../../config';
import { Col, Row, Grid } from 'react-native-easy-grid';
import Toast from 'react-native-simple-toast'; //this causes the error

请帮我解决这个问题。

最佳答案

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

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

确保将此文件添加到 jest 配置下的 package.json

"jest": 
.....
"setupFiles": [
"/setup.js" // location of your setup.js file
]

关于unit-testing - 类型错误 : Cannot read property 'SHORT' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51946583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com