Skip to content

[Bug]: toMatchObject on document.body makes Jest freeze #14133

Description

@elias6

Version

29.5.0

Steps to reproduce

  1. Clone the repo at https://github.com/elias6/jestFreezeTest.
  2. Run yarn.
  3. Run yarn test.
  4. Note that you see about to expect in your terminal, but not done expecting.

Expected behavior

I expect the assertion to pass and the test to finish running.

Actual behavior

Jest uses a lot of CPU, and the expect(document.body).toMatchObject(document.body); line never seems to finish.

If you try to exit Jest by pressing Ctrl+C, the process might still be running and using a lot of CPU, and not exit unless you kill it. This does not always happen though.

Additional context

I am able to reproduce this in Jest 29.2.2 (with [email protected]) and Jest 29.5.0 (with [email protected]). I did not try any other versions.

If you want a more realistic example of a real-world situation where this can happen, you can change the assertion to something like expect({ element: document.body }).toMatchObject({ element: document.body });, but this is not necessary.

My real-world test suite passes in Jest 29.2.2, but freezes in Jest 29.5.0, for reasons I haven't figured out.

If I remove one or more of the buttons, Jest takes a somewhat long time, then shows output like this:

yarn run v1.22.19
$ jest
  console.log
    about to expect

      at Object.log (test.js:18:11)

 FAIL  ./test.js (17.754 s)
  something
    ✕ does something (17359 ms)

  ● something › does something

    TypeError: undefined is not a function

      17 |
      18 | 		console.log("about to expect");
    > 19 | 		expect(document.body).toMatchObject(document.body);
         | 		                      ^
      20 | 		console.log("done expecting");
      21 | 	});
      22 | });

      at Object.toMatchObject (test.js:19:25)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        18.429 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Environment

System:
    OS: macOS 11.7.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 18.13.0 - /usr/local/opt/node@18/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 8.19.3 - /usr/local/opt/node@18/bin/npm
  npmPackages:
    jest: ^29.5.0 => 29.5.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions