react-testing-library version: 7.0.1react version: 16.8.6node version: 12.2.0npm (or yarn) version: npm 6.9.0jest version: 24.8.0ts-jest version: 24.0.2Here鈥檚 the Jest config and test file that鈥檚 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
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.
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鈥攊t fails before it gets to that step_.
Full reproduction repo here: https://github.com/dangodev/rtl-create-element
document.createElement seems to be undefined inside react-testing-library. Or I have Jest misconfigured?
My first thought was that somehow I just wasn鈥檛 using jsdom. But this is the default env now, and console.log(document.createElement) works fine in the test file. However, either that鈥檚 undefined in react-testing-library. Or the React.default.createElement is somehow defined鈥攏ot sure.
Perhaps there鈥檚 something missing in my Jest config, but if that鈥檚 the case maybe we can add a section to the setup docs?
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! 馃槃
Thanks so much! 馃憦 And yes, sorry鈥攚ill 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:
import * as React from 'react';
No troubles though, happy to help!
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
Most helpful comment
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! 馃槃