【发布时间】:2018-05-02 13:11:39
【问题描述】:
我正在尝试在 React with Enzyme 中设置单元测试。当我运行命令“npm-test”时,测试失败。 控制台终端指示由于 shallow() 而失败。 我已经使用这个命令安装了酵素 npm install --save 酵素酵素-adapter-react-16 react-test-renderer。有谁知道如何解决这个问题?
下面是组件
import React from 'react';
class Login extends Component {
render() {
return <div><input
onChange={(event) => {
this.setState({input: event.target.value})}}
type="text" /></div>;
}
}
export default Login;
这是我为组件编写的单元测试。
import React from 'react';
import { configure, shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import Login from '../../../src/components/authentication/Login';
import Enzyme from 'enzyme';
configure({adapter: new Adapter()});
it("should create an entry in component state with the event value", () => {
// given
const component = shallow(<Login/>);
const form = component.find('input');
// when
form.props().onChange({target: {
name: 'myName',
value: 'myValue'
}});
// then
expect(component.state('input')).toEqual('myValue');
});
感谢您的帮助。
【问题讨论】:
-
那么实际的错误信息是什么?
-
酶适配器反应 16 可能存在一些问题。看起来他们有 311 个未解决的问题:github.com/airbnb/enzyme/… 我用 15.4 运行了你的代码,没有任何问题。