Search code examples
reactjsreduxmocha.jscreate-react-app

Can't run mocha tests with create-react-app


I am trying to run tests using mocha for my create-react-application generated react/redux application.But I am getting this error:

import leaveReducer from '../reducers/leave.reducer.js'                                                          

^^^^^^                                                                                                           


SyntaxError: Unexpected token import                                                                             
   at createScript (vm.js:80:10)                                                                                
   at Object.runInThisContext (vm.js:152:10)                                                                    
   at Module._compile (module.js:624:28)                                                                        
   at Object.Module._extensions..js (module.js:671:10)                                                          
   at Module.load (module.js:573:32)                                                                            
   at tryModuleLoad (module.js:513:12)                                                                          
   at Function.Module._load (module.js:505:3)                                                                   
   at Module.require (module.js:604:17)                                                                         
   at require (internal/module.js:11:18) 

I can't write ES6 syntax in my test.I think i am missing some kind of configurations to be done to the application. Thank you.


Solution

  • You need to modify your package.json file or .babelrc as

    scripts: {
      "test": "./node_modules/.bin/mocha --require babel-register 'PATH TO YOUR TEST DIR OR FILE' "
      }