Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
drwpow opened this issue May 20, 2019 · 6 comments
Closed

Comments

@drwpow
Copy link

drwpow commented May 20, 2019

  • 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?

@bcarroll22
Copy link
Contributor

bcarroll22 commented May 21, 2019

hi @DangoDev, the issue is that your tsconfig.json file should contain the following:

{
  "compilerOptions": {
    "jsx": "react",
    "lib": ["dom", "esnext"],
    "module": "esnext",
    "esModuleInterop": true
  },
  "exclude": ["node_modules"]
}

After making this change, your repro test passes. In the future, could you do us a favor and ask support questions like this on our Spectrum chat? We'd really appreciate it. Thanks, and good luck! 😄

@drwpow
Copy link
Author

drwpow commented May 21, 2019

Thanks so much! 👏 And yes, sorry—will do that in the future.

By chance is this TypeScript setup anywhere in the documentation, and I missed it? If not, is there a good place to add this?

@bcarroll22
Copy link
Contributor

It wouldn't be in our docs, that fix isn't related to this library. It's just a general requirement for using React with TypeScript. Without that flag, you have to import React this way:

import * as React from 'react';

No troubles though, happy to help!

lucbpz pushed a commit to lucbpz/react-testing-library that referenced this issue Jul 26, 2020
* docs: update README.md

* docs: update .all-contributorsrc

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
@andrescabana86
Copy link

Hello here, I have asked in Discord with no luck. I tried with the solution above nothing seems to be working

same problem

this is my repo -> https://github.com/andrescabana86/epic-react-boilerplate

@mareksuscak
Copy link

Possibly related for anyone running into this issue:
testing-library/react-hooks-testing-library#294

The only solution that worked for me is:
testing-library/react-hooks-testing-library#294 (comment)

jaketrent added a commit to jaketrent/ts-esm-jest-react-storybook-poc that referenced this issue Mar 19, 2021
- Remove a lot of util
- Rename .story to .stories
- Rename .spec to .test
- Remove storyshots - started to get unexpected token errors for code
  that tested just fine beforehand. Possibly related: storybookjs/storybook#9470
- Adjusted jest.config.js to add esModuleInterop. Previously was getting
  Cannot read createElement of undefined errors. A description: testing-library/react-testing-library#374
jaketrent added a commit to jaketrent/ts-esm-jest-react-storybook-poc that referenced this issue Mar 19, 2021
- Remove a lot of util
- Rename .story to .stories
- Rename .spec to .test
- Set test env to jsdom
- Remove storyshots - started to get unexpected token errors for code
  that tested just fine beforehand. Possibly related: storybookjs/storybook#9470
- Adjusted jest.config.js to add esModuleInterop. Previously was getting
  Cannot read createElement of undefined errors. A description: testing-library/react-testing-library#374
bobbyquennell added a commit to bobbyquennell/peggy that referenced this issue Aug 9, 2021
- enhance jest.config.js
 1. add moduleDirectories: ['node_modules', 'src'], so jest can find modules in src folder
 instead of writing test like this: import greeter from './greeter'
 we can: import greeter from 'greeter';
 2. add setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts']
 to support extended handy expect
 check: testing-library/react-testing-library#379 (comment)
- add "esModuleInterop": true in tsconfig.json
  to fix: Cannot read property 'createElement' of undefined using TypeScript
   testing-library/react-testing-library#374 (comment)
   esModuleInterop vs allowSyntheticDefaultImports
   note: Enabling esModuleInterop will also enable allowSyntheticDefaultImports.
- add a new text example for react component: App.test.tsx
@andirsun
Copy link

Dont forget to import React on .tsx files

Before

import {FunctionComponent} from 'react'

After

import React, {FunctionComponent} from 'react'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants