Skip to content

Latest commit

 

History

History
53 lines (39 loc) · 1.79 KB

prefer-focus.md

File metadata and controls

53 lines (39 loc) · 1.79 KB

Prefer toHaveFocus over checking document.activeElement (jest-dom/prefer-focus)

💼 This rule is enabled in the ✅ recommended config.

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

prefer toHaveFocus over checking document.activeElement (prefer-focus)

Rule Details

This autofixable rule aims to improve readability & consistency of tests with the use of the jest-dom matcher toHaveFocus rather than checking document.activeElement.

Examples of incorrect code for this rule:

expect(document.activeElement).toBe(foo);
expect(window.document.activeElement).toBe(foo);
expect(global.window.document.activeElement).toBe(foo);
expect(global.document.activeElement).toBe(foo);
expect(foo).toBe(global.document.activeElement);
expect(foo).toBe(window.document.activeElement);
expect(foo).toBe(global.window.document.activeElement);
expect(foo).toBe(document.activeElement);
expect(foo).toEqual(document.activeElement);

expect(document.activeElement).not.toBe(foo);
expect(window.document.activeElement).not.toBe(foo);
expect(global.window.document.activeElement).not.toBe(foo);
expect(global.document.activeElement).not.toBe(foo);
expect(foo).not.toBe(global.document.activeElement);
expect(foo).not.toBe(window.document.activeElement);
expect(foo).not.toBe(global.window.document.activeElement);
expect(foo).not.toBe(document.activeElement);
expect(foo).not.toEqual(document.activeElement);

Examples of correct code for this rule:

expect(input).not.toHaveFocus();
expect(document.activeElement).not.toBeNull();

expect(input).toHaveFocus();
expect(document.activeElement).toBeNull();

When Not To Use It

If you don't care if people use toHaveFocus