jest: [Bug]: `document` is `null` during microtasks at the end of a test

Version

27.5.1

Steps to reproduce

  1. clone https://github.com/eps1lon/react-testing-library-error-repro/tree/jest-repro
  2. checkout jest-repro branch
  3. yarn install
  4. yarn test

Expected behavior

Test fails due to timeout.

Actual behavior

document is null during a microtask scheduled from a ReactDOMTestUtils.act call.

Additional context

Continued from https://github.com/facebook/jest/issues/9056#issuecomment-1098048482 which is a reduced repro of https://github.com/testing-library/react-testing-library/issues/1027

Environment

System:
    OS: Linux 5.13 Ubuntu 20.04.4 LTS (Focal Fossa)
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
  Binaries:
    Node: 14.18.3 - ~/.nvm/versions/node/v14.18.3/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v14.18.3/bin/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v14.18.3/bin/npm
  npmPackages:
    jest: ^27.5.1 => 27.5.1

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 43
  • Comments: 21 (6 by maintainers)

Most upvoted comments

I experienced this one today. Error is obtuse and stack trace is unhelpful, especially within a large team.

Some days I fight with Jest, some days I let Jest win.

I just gave up and deleted the spec.

Problem solved 😉

This error is difficult for non-react savvy folks to understand. It would be great to fix the issue and add more error messaging to direct folks on what to do about it a bit more clearly.

As discussed in https://github.com/testing-library/react-testing-library/issues/1027. This issue is experienced very often while running async tests on Github Actions CI. As a mitigation I add jest.runAllTimers() at the end of test to exhaust all micro/macro task queues, but it rarely works.

Also, I have tests which run successfully on local machine but fail only in Github Actions CI with:

      var evt = document.createEvent('Event');

I ended up patching react-dom in my node_modules locally and added some debugging code to identify the problematic test. You can see it at https://github.com/Zylphrex/react-testing-library-error-repro. It’s a little gross but I hope it helps.

As of #13972 (released in 29.5.0), we no longer set the document to null. So this shouldn’t be an issue anymore.

If it is, please open up a new issue.

After trying random things to solve this issue, I discovered that increasing test timeout (jest.setTimeout, 30000) or test('title', () => {...}, 30000)) fixed it for me. Disclaimer - I don’t fully understand how this helped and whether this is reliable. Would appreciate an explanation if this is the proper workaround 🙏

is there any WA for this issue?

@mjchang If you were facing the same problem, that should fix it.

The root cause of this problem when I initially opened up testing-library/react-testing-library#1027 was that the test timed out during one of the getBy* queries. Your choices here are to increase the timeouts or make your test run faster.

I believe what’s happening here is because the test had timed out, jest started to do tear downs (I’m not sure exactly what). And at some point after, the getBy* tries to poll for results again and encounters this particular error which is not helpful in determining what the actual problem was.