Jest: document.activeElement is always null after upgrading to jest 25.x

Created on 26 Mar 2020  路  2Comments  路  Source: facebook/jest

馃挜 Regression Report

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

Last working version

Worked up to version:
24.9

Stopped working in version:
25.0

To Reproduce

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.

Expected behavior

The above test to pass in all versions of jest

Link to repl or repo (highly encouraged)

See example code above

Run npx envinfo --preset jest

Paste 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 
Regression

Most helpful comment

Just upgraded Jest to v26 (so jsdom to v16) and unfortunately the bug is still there.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings