Open jjorissen52 opened 3 years ago
In case this information is helpful to someone else, I will add the shown error as text to make this page show up in search:
ReferenceError: React is not defined
.
Try adding
import React from 'react'
; in your test file
@akshayr20 React is imported in the first line of both tests. To be clear, the strange bit is that the tests work as long as jest.mock()
is not present anywhere in the test file. The presence of jest.mock()
breaks the tests even if it's present only in the form of a comment.
This might help https://github.com/evanw/esbuild/issues/412
Just got bitten by this today, and couldn't figure out why it persisted even with the mock commented out. This issue was a life(and sanity)saver. There is a workaround from #61 above, changing:
import React from "react"
to
import * as React from "react"
in the affected test files, which does indeed resolve the issue. For now, I'm just going to avoid using mocks in my little project, though.
🤦♂️ I spent almost an hour just to learn i can't use new Dock()
because it contains ock(
.
🤦♂️ I spent almost an hour just to learn i can't use
new Dock()
because it containsock(
.
I've met exactly the same problem. My code contains a function named "block" and jest will report that React is not defined when I call this function like block()
. And I found the problem could happen whenever there's ock(
in the code, even in comment.
This repository demonstrates the inclusion of
jest.mock()
(even in a comment) when using@testing-library/react
in conjunction withesbuild-jest
causes the test environment to break.Reproduction
// jest.mock()
//jest.mock()
Remedy
Use babel instead. For a demonstration: