jest/prefer-to-have-length 风格
它做了什么
为了获得更好的失败信息,应在断言对象的 length 属性时使用 toHaveLength()。
为什么这是不好的?
如果使用 toBe()、toEqual() 或 toStrictEqual() 来断言对象的 length 属性,此规则会触发警告。
示例
此规则的 错误 代码示例:
javascript
expect(files["length"]).toBe(1);
expect(files["length"]).toBe(1);
expect(files["length"])["not"].toBe(1);此规则的 正确 代码示例:
javascript
expect(files).toHaveLength(1);此规则与 eslint-plugin-vitest 兼容。要使用它,请在您的 .oxlintrc.json 中添加以下配置:
json
{
"rules": {
"vitest/prefer-to-have-length": "error"
}
}如何使用
通过配置文件或命令行启用此规则,可以使用:
json
{
"plugins": ["jest"],
"rules": {
"jest/prefer-to-have-length": "error"
}
}bash
oxlint --deny jest/prefer-to-have-length --jest-plugin