Skip to content

TypeError: Cannot read properties of null (reading 'createEvent') #1027

@Zylphrex

Description

@Zylphrex
  • @testing-library/react version: 12.1.4
  • Testing Framework and version: jest@27.5.1
  • DOM Environment: jsdom@16.7.0

Relevant code or config:

import React, {useEffect} from "react";
import { render } from "@testing-library/react";

function Component() {
  useEffect(() => () => {}, []); // make sure to return a cleanup 
  return <>x</>;
}

it("it", function () {
  const {findByText} = render(<Component />);
  return findByText("x").then(() => {
  // the test should have timed out at this point
    return findByText("x");
  });
}, 1); // force a timeout of 1ms so that by the time the second `findByText` is called, the test should've failed

What you did:

You can see the original failure in this github action. The original test is attached below from this pull request

it('allows adding up to GROUP_BY_LIMIT fields', async function () {
  renderTestComponent({
    query: {displayType: 'line'},
    orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
  });

  await screen.findByText('Group your results');

  // this block is synchronous and causes the test to timeout
  for (let i = 0; i < 19; i++) {
    userEvent.click(screen.getByText('Add Group'));
  }

  // this findAllByText executes after the test times out and is the source of the error
  expect(await screen.findAllByText('Select group')).toHaveLength(20);
  expect(screen.queryByText('Add Group')).not.toBeInTheDocument();
});

I've stripped down the component and test to a minimal case, hence why the test may seem a little strange.

I had a test that did 2 asynchronous findByText calls with some the synchronous calls in between that was causing the test to timeout.

What happened:

I was expecting a timeout error pointing at the specific test that had failed. Instead, my tests crashed with the following stacktrace.

/home/runner/work/sentry/sentry/node_modules/react-dom/cjs/react-dom.development.js:3905
      var evt = document.createEvent('Event');
                         ^
TypeError: Cannot read properties of null (reading 'createEvent')
    at Object.invokeGuardedCallbackDev (/home/runner/work/sentry/sentry/node_modules/react-dom/cjs/react-dom.development.js:3905:26)
    at invokeGuardedCallback (/home/runner/work/sentry/sentry/node_modules/react-dom/cjs/react-dom.development.js:4056:31)
    at flushPassiveEffectsImpl (/home/runner/work/sentry/sentry/node_modules/react-dom/cjs/react-dom.development.js:23543:11)
    at unstable_runWithPriority (/home/runner/work/sentry/sentry/node_modules/scheduler/cjs/scheduler.development.js:468:12)
    at runWithPriority$1 (/home/runner/work/sentry/sentry/node_modules/react-dom/cjs/react-dom.development.js:11276:10)
    at flushPassiveEffects (/home/runner/work/sentry/sentry/node_modules/react-dom/cjs/react-dom.development.js:23447:14)
    at Object.<anonymous>.flushWork (/home/runner/work/sentry/sentry/node_modules/react-dom/cjs/react-dom-test-utils.development.js:992:10)
    at Immediate.<anonymous> (/home/runner/work/sentry/sentry/node_modules/react-dom/cjs/react-dom-test-utils.development.js:1003:11)
    at processImmediate (node:internal/timers:464:21)

Reproduction:

You can find a repro in https://github.com/Zylphrex/react-testing-library-error-repro

Problem description:

This error was causing an error without any indication which test was the culprit. This should be failing the test with a timeout, and the test that timed out should obvious.

Suggested solution:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions