Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Jest测试失败,未定义警报_Reactjs_Unit Testing_React Native_Jestjs_Enzyme - Fatal编程技术网

Reactjs Jest测试失败,未定义警报

Reactjs Jest测试失败,未定义警报,reactjs,unit-testing,react-native,jestjs,enzyme,Reactjs,Unit Testing,React Native,Jestjs,Enzyme,我将jest用于我的react原生项目,我想测试具有onPress的组件。当点击它时,警报就会出现 <ListItem testID={'Contact'} onPress={() => alert('hello')} /> 并对其进行测试 it('test onPress functionality', () => { window.alert = jest.fn(); const wrapper = shallow(

我将jest用于我的react原生项目,我想测试具有onPress的组件。当点击它时,警报就会出现

<ListItem
      testID={'Contact'}
      onPress={() => alert('hello')}
/>
并对其进行测试

it('test onPress functionality', () => {
    window.alert = jest.fn();
    const wrapper = shallow(
      <Contact
        onPress={window.alert}
      />,
    );
    wrapper
      .findWhere(node => node.prop('testID') === 'Contact')
      .props()
      .onPress();
    expect(window.alert).toHaveBeenCalledWith('hello');
  });
但是这个测试给出了警告错误

ReferenceError:未在Press={=>alert'hello'}上定义警报


请帮助我修复此错误。

您的测试不正确,因为您测试的参数是“onPress”

expect(window.alert).toHaveBeenCalledWith('hello');

上述操作应该有效。

。请使用react native Alert

import {Alert} from 'react-native';

<ListItem
      testID={'Contact'}
      onPress={() => Alert.alert('hello')}
/>

检查文档

多亏了修复,但错误与此无关,现在我修复了,仍然会出现相同的错误。