reactjs - 反应单元测试 - Enzmye shallow
问题描述
我正在尝试在 React with Enzyme 中设置单元测试。当我运行命令“npm-test”时,测试失败。控制台终端指示由于 shallow() 而失败。我已经使用这个命令安装了酶 npm install --save 酶酶-适配器-反应-16 反应-测试-渲染器。有谁知道如何解决这个问题?
下面是组件
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');
});
谢谢您的帮助。
解决方案
嗨,也许你错过了约定:
您必须将测试文件放在一个__tests__
目录中,并且测试文件应命名为:YourComponent.test.js
比将您的测试包装在测试套件中:
describe('Your test suite', () => {
test('it should do something', () => {
// the content of your test
});
});
推荐阅读
- sql - 如何在 SQL Server 中将字符串转换为日期
- c# - 访问要在 IValueResolver 中映射的目标属性名称
- javascript - 过滤器显示/隐藏元素的问题
- mysql - MySQL Cancel JOIN on CASE 不匹配
- loops - 为什么我可以同时使用 for 循环和插入符号的 train() 函数?
- windows - Visual Studio Code - 无法创建进程,错误代码 5
- docker - 在运行 Dockerfile 时有可用的命令行
- reactjs - TypeError:无法在 next.js 中读取未定义的属性(读取“地图”)
- kubernetes - k3s MetalLB ingress-nginx裸机服务器入口问题
- continuous-integration - 如何制作 ivy.xml 文件和 ivysettings.xml