0

After surfing the forums , I reconfirmed the usage of proper imports as mentioned below:

import React from 'react';   
import ReactDOM from 'react-dom';

Also tried removing the following lines from webpack.config.js:

externals: [
          
  {
 react: 'React',},
        
],

But still am getting the following error, while running jest testcases

ReferenceError: React is not defined

brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
codeX
  • 4,842
  • 2
  • 31
  • 36
  • Why are you importing React with template literal `import React from 'react';` ?? Why are you importing React multiple times? Have you installed React? Please provide your component code otherwise it’s not easy to help you with the solution – Hemadri Dasari Feb 21 '18 at 14:19
  • What does your `package.json` look like? – Sergii Vorobei Feb 21 '18 at 14:21
  • @HemadriDasari import React from 'react' , will load react – codeX Feb 21 '18 at 14:25
  • The webpack instruction `react: 'React'` may confuse and affect such error. If react is not installed nor allowed will be another erorr. `React is not defined` mean thar import module is resolved but it exports does not contain default value which wants as `React` variable. – oklas Feb 21 '18 at 14:26
  • Are you using Jest with webpack? – Adarsh Feb 21 '18 at 14:28
  • @oklas I agree . Thus I tried removing the statement but it earned no use for me here . – codeX Feb 21 '18 at 14:29
  • @Aadi Yeah . Am using Jest with webpack – codeX Feb 21 '18 at 14:30
  • Did you check webpack conf (and all includes) and restart webpack devserver? – oklas Feb 21 '18 at 14:30
  • yeah @oklas . I did . – codeX Feb 21 '18 at 14:32
  • Are you having a Jest config of sorts. If so can you post it's configuration – Adarsh Feb 21 '18 at 14:32
  • Check manually what is `node_modules/react` exports looks like. May be some error during installation and you have empty file. Also `rm -rf node_modules && npm install` helps in such cases. – oklas Feb 21 '18 at 14:35
  • @oklas Tried rm -rf node_modules && npm install already ! – codeX Feb 21 '18 at 14:36
  • Did you use a git Coud you rollback to stable version? Check also `.babelrc` about react. (Also Post jest config as @Aadi requested let him to see.) Most probably we need manual access to project. – oklas Feb 21 '18 at 14:41
  • I have contact at my profile page if any. – oklas Feb 21 '18 at 14:41
  • @oklas Thanks . Will contact if i am not really able to solve it in few more hours . – codeX Feb 21 '18 at 14:54
  • Possible duplicate of [Uncaught ReferenceError: React is not defined](https://stackoverflow.com/questions/32070303/uncaught-referenceerror-react-is-not-defined) – Zach Saucier Aug 16 '19 at 20:50

1 Answers1

0

For jest + react change to import * as React from 'react'

jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
Ziv Barber
  • 595
  • 9
  • 13