运行玩笑测试时获得失败状态?

时间:2020-01-28 07:21:00

标签: reactjs jestjs tdd

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

encoding

我的 ● 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); }); });

login component

1 个答案:

答案 0 :(得分:0)

错误日志显示它收到的是object而不是长度。因此,您可以做的更改是:

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

import { mount } from 'enzyme';



 const wrapper = mount(<Provider store={store}>
                             <Login />
                         </Provider>);

mount将生成所有嵌套组件的树(如果有)。

现在正在测试中:

it('should have 3 input fields !', ()=> {            
    expect(wrapper.find('input').length).toHaveLength(3); //<----check for length          
});