We use expect(document.activeElement).toBe(myElement) pattern to determine if an element got correctly focused after an interaction. After upgrading to jest 25 all such tests broke, document.activeElement appears to be always null
Worked up to version:
24.9
Stopped working in version:
25.0
Steps to reproduce the behavior:
import * as React from 'react';
it('should find active element', () => {
const wrapper = mount(<input />);
const inputNode = wrapper.find('input').getDOMNode();
expect(document.activeElement).not.toBe(inputNode);
inputNode.focus();
expect(document.activeElement).toBe(inputNode);
});
Run the above test with jest 24 - it passes.
Run the above test with jest 25 - it fails.
The above test to pass in all versions of jest
See example code above
npx envinfo --preset jestPaste the results here:
For passing tests:
System:
OS: macOS 10.15.3
CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Binaries:
Node: 10.19.0 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.13.4 - /usr/local/bin/npm
npmPackages:
jest: 24.8.0 => 24.8.0
After upgrade:
System:
OS: macOS 10.15.3
CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Binaries:
Node: 10.19.0 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.13.4 - /usr/local/bin/npm
npmPackages:
jest: 25.2.1 => 25.2.1
We've upgraded jsdom, which is a library responsible for web-like environment (see https://jestjs.io/docs/en/configuration#testenvironment-string), from v11 to v15. Please try the latest v16 available through https://www.npmjs.com/package/jest-environment-jsdom-sixteen and if that still is a bug, please provide a clear repro and create an issue here: https://github.com/jsdom/jsdom
Just upgraded Jest to v26 (so jsdom to v16) and unfortunately the bug is still there.
Most helpful comment
Just upgraded Jest to v26 (so jsdom to v16) and unfortunately the bug is still there.