-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
hi @DangoDev, the issue is that your tsconfig.json file should contain the following:
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! 😄 |
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? |
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:
No troubles though, happy to help! |
* docs: update README.md * docs: update .all-contributorsrc Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
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 |
Possibly related for anyone running into this issue: The only solution that worked for me is: |
- 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
- 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
- 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
Dont forget to import React on .tsx files Before
After
|
react-testing-library
version:7.0.1
react
version:16.8.6
node
version:12.2.0
npm
(oryarn
) 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:
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 acreateElement not found
error. Runningnpm test
triggers this.What happened:
The test suite errs:
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 beundefined
insidereact-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, andconsole.log(document.createElement)
works fine in the test file. However, either that’s undefined inreact-testing-library
. Or theReact.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?
The text was updated successfully, but these errors were encountered: