Skip to content

Latest commit

 

History

History
44 lines (30 loc) · 1.46 KB

prefer-to-have-value.md

File metadata and controls

44 lines (30 loc) · 1.46 KB

Prefer toHaveValue over checking element.value (jest-dom/prefer-to-have-value)

💼 This rule is enabled in the ✅ recommended config.

🔧 This rule is automatically fixable by the --fix CLI option.

This rule is an autofixable rule that encourages the use of toHaveValue over checking the value attribute.

Rule Details

This rule checks for usages of .value or .toHaveAttribute("value"). The only valid use case is when using greater/less than matchers since there isn't any equivalent use with toHaveValue

Examples of incorrect code for this rule:

expect(element).toHaveAttribute("value", "foo");
expect(element).toHaveProperty("value", "foo");
expect(element.value).toBe("foo");
expect(element.value).not.toEqual("foo");
expect(element.value).not.toStrictEqual("foo");

Examples of correct code for this rule:

expect(element).toHaveValue("foo");
expect(element.value).toBeGreaterThan(2);
expect(element.value).toBeLessThan(2);

When Not To Use It

If you don't care about using built in matchers for checking attributes on dom elements.

Further Reading