-
Notifications
You must be signed in to change notification settings - Fork 51
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
ReferenceError: React is not defined #61
Comments
And why is something like "@babel/preset-typescript" needed for this to work ? I can build the whole project with esbuild-loader without having to use Babel ?! |
I've ran into the same problem just today. I've tried out various different solutions, which have included:
I've seemed to have tracked it down to this error
All of my test files have The error seems to come when the shallow render takes place inside the test...
The interesting part is that I've been running multiple tests and there's no difference between the tests or components. So Test A will always pass and Test B will always fail with this error. I've removed Test A from even running (commented out), but this doesn't seem to help. My config is as follows: {
"rootDir": "../",
"setupFilesAfterEnv": ["./jest/setup.js"],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)$": "identity-obj-proxy",
"\\.(css|scss)$": "identity-obj-proxy"
},
"coverageDirectory": "coverage",
"snapshotSerializers": ["enzyme-to-json/serializer"],
"cacheDirectory": "./jest/cache",
"testEnvironment": "jsdom",
"transform": {
"\\.(js|jsx|ts|tsx)$": "./jest/transformer.js"
},
"watchPlugins": ["jest-watch-typeahead/filename", "jest-watch-typeahead/testname"]
}
with the transformer setup as const esbuildJest = require('esbuild-jest');
module.exports = esbuildJest.createTransformer({
sourcemap: true,
loaders: {
'.js': 'jsx',
},
}); Any help would be greatly appreciated! Versions
|
@vernak2539 hey, the error occurs regardless of the used method (shallow(), mount() from enzyme or render() from testing-library). All other tests are using the same methods and are passing. The issue seem to occur when using something like this in the test: jest.mock("react-router-dom", () => ({
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
...jest.requireActual("react-router-dom"),
useHistory: () => ({
replace: mockHistoryReplace,
push: mockHistoryPush,
}),
})); |
Ahh yeah, interesting. My test looks like this: import React from 'react';
import { shallow } from 'enzyme';
import fetch from '@custom/fetch';
import DataLoaderExample from './index';
jest.mock('@custom/fetch', () => jest.fn());
describe('DataLoaderExample', () => {
it('should render initial button correctly', () => {
const wrapper = shallow(<DataLoaderExample />);
expect(wrapper.find('withLayout(withSpacing(Button))').html()).toContain(
'Load Sample API Data',
);
});
}); When removing the line |
This looks like the same issue maybe? |
@vernak2539 yes, and furthermore the linked issue evanw/esbuild#412 But how can this be fixed ? |
I found that changing the import statement to |
I've run into this same issue. If I include This passes: import React from "react";
import { shallow } from "enzyme";
it("should pass", () => {
const wr = shallow(<div></div>);
}); This fails: import React from "react";
import { shallow } from "enzyme";
jest.mock("lodash"); // <<--------- added this line
it("should pass", () => {
const wr = shallow(<div></div>);
}); If I then amend import * as React from "react"; // <<--------- amended this line
import { shallow } from "enzyme";
jest.mock("lodash");
it("should pass", () => {
const wr = shallow(<div></div>);
}); |
Same here, changing |
Im facing the same error as I relate here https://twitter.com/daniloab_/status/1497258702662823936. My test has a simple react component to be used as a root because the function being tested needs some hooks. The test breaks because we do not import the react since react 17 does not need anymore to do it. Does anyone know how to fix it? |
This also seems related to #65. Adding the following to a file referenced in import React from "react";
// whatever else you need in here
global.React = React; |
@llwt wow that's a lot better, at least it's just in one file... thanks man |
@albertodeago Np. Not sure if you specifically need to use I normally wouldn't recommend a different package in the issues of another, but this one seems unmaintained 🤷🏻 |
Yeah I know it, I was just giving esbuild a shot for a side-project, thx for the suggestion anyway |
For those still coming across this issue, I fixed it by adding My
|
I use CRA without babel config |
Stumbled upon this, then found esbuild already provides an option for automatic React import with |
for node environment |
hey there,
i've just installed esbuild-jest and figured most of the issues out myself, but as you can see here:
20 tests / 4 test files are failing with error message:
ReferenceError: React is not defined
All of them are tsx files and have the same import of import React from "react";
Has someone an idea ?
All tests are passing with ts-jest
The text was updated successfully, but these errors were encountered: