Testing for focused element with jest 25.x

Viewed 5453

We use jest tests with expect(document.activeElement).toBe(myElement) pattern to determine if an element got correctly focused after an interaction. After upgrading to jest 25 (jsdom 15) all such tests broke, document.activeElement appears to be always null.

For example the following test works with Jest 24.x but not with 25.x

// example.spec.js
import * as React from 'react';

it('should find active element', () => {
    const wrapper = mount(<input />);
    const inputNode = wrapper.find('input').getDOMNode();
    inputNode.focus();
    expect(document.activeElement).toBe(inputNode); // fails with 'expected' being null
});

What is the best way to test if an element is focused using Jest 25.x?

0 Answers
Related