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
});