Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.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_Jestjs_Tdd - Fatal编程技术网

Reactjs 运行jest测试时获取失败状态?

Reactjs 运行jest测试时获取失败状态?,reactjs,jestjs,tdd,Reactjs,Jestjs,Tdd,我试图在我的应用程序中编写第一个测试,但出现如下错误 ● Login component tests › should have 3 input fields ! expect(received).toHaveLength(expected) Expected length: 3 Received length: 0 Received object: {} 13 | 14 | it('should have 3 input fields !', ()=>

我试图在我的应用程序中编写第一个测试,但出现如下错误

 ● Login component tests › should have 3 input fields !

expect(received).toHaveLength(expected)

Expected length: 3
Received length: 0
Received object: {}

  13 | 
  14 |         it('should have 3 input fields !', ()=> {
> 15 |             expect(wrapper.find('input')).toHaveLength(3);
     |                                           ^
  16 |         });
  17 | 
  18 | 

  at Object.it (src/components/auth/Login.test.js:15:43)

Test Suites: 1 failed, 1 total
我的
Login.test.js
文件是

    import React from 'react';
import { shallow } from 'enzyme';
import Login from './Login';
import {Provider} from 'react-redux';
import store from '../../../src/store';

    describe('Login component tests', ()=> {
        const wrapper = shallow(
                                <Provider store={store}>
                                    <Login />
                                </Provider>
                        );

        it('should have 3 input fields !', ()=> {            
            expect(wrapper.find('input')).toHaveLength(3);           
        });



    });

正如错误日志所说,它接收的是
对象
,而不是长度。因此,您可以做的更改是:

我想第一个必须是
mount
而不是
shall

import { mount } from 'enzyme';



 const wrapper = mount(<Provider store={store}>
                             <Login />
                         </Provider>);
从“酶”导入{mount};
常量包装器=装入(
);
mount将生成所有嵌套组件(如果有)的树

现在在测试中:

it('should have 3 input fields !', ()=> {            
    expect(wrapper.find('input').length).toHaveLength(3); //<----check for length          
});
it('应该有3个输入字段!',()=>{

expect(wrapper.find('input').length).toHaveLength(3);//能否添加登录组件。匹配器错误:收到的值必须具有一个length属性,其值必须为数字
it('should have 3 input fields !', ()=> {            
    expect(wrapper.find('input').length).toHaveLength(3); //<----check for length          
});