Skip to content

TypeError: Cannot read property 'createElement' of undefined using TypeScript #374

Closed
@drwpow

Description

@drwpow
  • react-testing-library version: 7.0.1
  • react version: 16.8.6
  • node version: 12.2.0
  • npm (or yarn) version: npm 6.9.0
  • jest version: 24.8.0
  • ts-jest version: 24.0.2

Relevant code or config:

Here’s the Jest config and test file that’s failing:

module.exports = {
  preset: 'ts-jest',
  setupFilesAfterEnv: ['react-testing-library/cleanup-after-each'],
};
import React from "react";
import { render } from "react-testing-library";
import "jest-dom/extend-expect";
import App from "./App";

test("NotFound page", async () => {
  const { getByTestId } = render(<App />);
  expect(getByTestId("title")).toHaveTextContent("Hello CodeSandbox");
});

The rest of the example can be found here: https://github.com/dangodev/rtl-create-element

What you did:

Setting up react-testing-library in a new project, with the latest versions of Jest, TypeScript, and React, encounters a createElement not found error. Running npm test triggers this.

What happened:

The test suite errs:

TypeError: Cannot read property 'createElement' of undefined

   5 |
   6 | test("NotFound page", async () => {
>  7 |   const { getByTestId } = render(<App />);
     |                                  ^
   8 |   expect(getByTestId("title")).toHaveTextContent("Hello CodeSandbox");
   9 | });
  10 |

Note: this will err without the object destructuring—it fails before it gets to that step.

Reproduction:

Full reproduction repo here: https://github.com/dangodev/rtl-create-element

Problem description:

document.createElement seems to be undefined inside react-testing-library. Or I have Jest misconfigured?

Suggested solution:

My first thought was that somehow I just wasn’t using jsdom. But this is the default env now, and console.log(document.createElement) works fine in the test file. However, either that’s undefined in react-testing-library. Or the React.default.createElement is somehow defined—not sure.

Perhaps there’s something missing in my Jest config, but if that’s the case maybe we can add a section to the setup docs?

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