【问题标题】:TypeError: Cannot read property 'params' of undefined Jest Testing - React NativeTypeError:无法读取未定义的 Jest 测试的属性“参数”-React Native
【发布时间】:2021-03-11 01:52:13
【问题描述】:

我创建了一个测试文件来测试我的以下“BiddingScreen”,

const BiddingScreen = ({ route, navigation }) => {

const { currentBid } = route.params;


return (
    <SafeAreaView style={styles.main}>
        <Header title="BID Here" navigation={navigation} />
        <View style={styles.container}>
            <Text style={styles.currentBid}>{CurrentBid}$</Text>
        </View>
    </SafeAreaView>
)}

我的测试文件如下,

import React from 'react';
import renderer from 'react-test-renderer';
import BiddingScreen from "../../../src/containers/biddingScreen/BiddingScreen";

jest.mock('@react-native-firebase/auth');

jest.mock("react-native/Libraries/EventEmitter/NativeEventEmitter");


test('renders correctly', () => {
    const tree = renderer.create(< BiddingScreen />).toJSON();
    expect(tree).toMatchSnapshot();
});

当我运行我的测试文件时,我得到了这个错误,

TypeError: Cannot read property 'params' of undefined

谁能帮我解决这个问题,谢谢

【问题讨论】:

    标签: react-native jestjs babel-jest react-test-renderer


    【解决方案1】:

    只要您的组件道具没有默认值,您就需要模拟它们,例如:

    describe('init', () => {
      test('renders correctly', () => {
        const mockedParams = {
          route: { params: { currentBid: 'whatever-id' } },
          navigation: ''
        };
    
        const tree = renderer.create(<BiddingScreen {...mockedParams} />).toJSON();
        expect(tree).toMatchSnapshot();
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-08
      • 2017-06-27
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 2019-02-17
      • 2020-11-05
      • 2020-04-17
      相关资源
      最近更新 更多