【发布时间】:2020-02-20 02:53:44
【问题描述】:
试图让登录单元测试正常工作,但它一直给我错误
Enzyme 内部错误:Enzyme 需要配置适配器,但没有找到。
要配置适配器,您应该调用Enzyme.configure({ adapter: new Adapter() })
在使用任何 Enzyme 的顶级 API 之前,其中 Adapter 是适配器
对应于当前正在测试的库。例如:
从 'enzyme-adapter-react-15' 导入适配器;
这是我的单元测试
import React from 'react';
import { shallow, mount, render } from 'enzyme';
import Login from './components/login.js';
describe('Login Test Suite', () => {
it('should render the form', () => {
const wrapper = shallow(<Login />);
expect(wrapper.find('form.login').exists()).toBe(true);
expect(wrapper.find('#Username').length).toEqual(1);
expect(wrapper.find('#password').length).toEqual(1);
})
})
describe('Username Test Suite', () => {
it('should change the state of the Login component', () => {
const wrapper = shallow(<Login />);
wrapper.find('#Username').simulate('blur',
{
target: { name: 'Username', value: 'adastest' }
});
expect(wrapper.state('Username')).toEqual('adastest');
})
})
describe('Password Test Suite', () => {
it('should change the state of the Login component', () => {
const wrapper = mount(<Login />);
wrapper.find('#password').simulate('blur',
{
target: { name: 'password', value: 'adastest' }
});
expect(wrapper.state('password')).toEqual('adastest');
})
})
【问题讨论】: