babel-register not working in mocha test, nodejs react

1.1k views Asked by At

Hi I am struggling for a week solving this Unexpected token 'import' error.

After tons of searching, now I know that this is a problem of babel-register which is not applying to mocha test, while webpack serve works fine.

At first I had an Unexpected token 'import' error when using antd library and I followed other solutions which told me to set .babelrc(setting node_modules: false)

But now I am getting same Unexpected token 'import' error from my test file. Please help me... This is not a duplicate and I've done almost every solution in the web related to this. I'm pretty sure that --require babel-register is not working in mocha test. Thank you.

myerror message

CSE_frontend choihongsuk$ npm run test

[email protected] test /Users/choihongsuk/Desktop/-13-SNUCSE-website-SWPP-/CSE_frontend BABEL_ENV=test mocha --require babel-hook --require babel-register --require ./test/test_hel per.js --recursive ./test

babel-preset-env: DEBUG option Using targets: {}

Modules transform: false

Using plugins: check-es2015-constants {}
transform-es2015-arrow-functions {}
transform-es2015-block-scoped-functions {}
transform-es2015-block-scoping {} transform-es2015-classes {}
transform-es2015-computed-properties {}
transform-es2015-destructuring {} transform-es2015-duplicate-keys {} transform-es2015-for-of {} transform-es2015-function-name {}
transform-es2015-literals {} transform-es2015-object-super {}
transform-es2015-parameters {} transform-es2015-shorthand-properties {} transform-es2015-spread {} transform-es2015-sticky-regex {}
transform-es2015-template-literals {} transform-es2015-typeof-symbol {} transform-es2015-unicode-regex {} transform-regenerator {}
transform-exponentiation-operator {} transform-async-to-generator {} syntax-trailing-function-commas {} /Users/choihongsuk/Desktop/-13-SNUCSE-website-SWPP-/CSE_frontend/test/test_helper.js:1 (function (exports, require, module, __filename, __dirname) { import _$ from 'jquery'; ^^^^^^

SyntaxError: Unexpected token import at new Script (vm.js:51:7) at createScript (vm.js:136:10) at Object.runInThisContext (vm.js:197:10) at Module._compile (module.js:613:28) at loader (/Users/choihongsuk/Desktop/-13-SNUCSE-website-SWPP-/CSE_frontend/node_modules/babel-register/lib/node.js:144:5) at Object.require.extensions.(anonymous function) [as .js] (/Users/choihongsuk/Desktop/-13-SNUCSE-website-SWPP-/CSE_frontend/node_modules/babel-register/lib/node.js:154:7) at Module.load (module.js:561:32) at tryModuleLoad (module.js:501:12) at Function.Module._load (module.js:493:3) at Module.require (module.js:593:17) at require (internal/module.js:11:18) at /Users/choihongsuk/Desktop/-13-SNUCSE-website-SWPP-/CSE_frontend/node_modules/mocha/bin/_mocha:366:3 at Array.forEach () at Object. (/Users/choihongsuk/Desktop/-13-SNUCSE-website-SWPP-/CSE_frontend/node_modules/mocha/bin/_mocha:365:10) at Module._compile (module.js:649:30) at Object.Module._extensions..js (module.js:660:10) at Module.load (module.js:561:32) at tryModuleLoad (module.js:501:12) at Function.Module._load (module.js:493:3) at Function.Module.runMain (module.js:690:10) at startup (bootstrap_node.js:194:16) at bootstrap_node.js:666:3 npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] test: BABEL_ENV=test mocha --require babel-hook --require babel-register --require ./test/test_helper.js --recursive ./test npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] test script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR!
/Users/choihongsuk/.npm/_logs/2018-05-25T03_45_58_998Z-debug.log

package.json

{
      "name": "redux-simple-starter",
      "version": "1.0.0",
      "description": "Simple starter package for Redux with React and Babel support",
      "main": "index.js",
      "repository": "[email protected]:StephenGrider/ReduxSimpleStarter.git",
      "scripts": {
        "start": "webpack-dev-server --progress --colors --open --hot",
        "test": "BABEL_ENV=test mocha --require babel-hook --require babel-register --require ./test/test_helper.js --recursive ./test",
        "test:watch": "npm run test -- --watch"
      },
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "babel-core": "^6.26.3",
        "babel-loader": "^6.4.1",
        "babel-plugin-import": "^1.7.0",
        "babel-plugin-transform-class-properties": "^6.24.1",
        "babel-plugin-transform-decorators-legacy": "^1.3.4",
        "babel-plugin-transform-runtime": "^6.23.0",
        "babel-preset-env": "^1.7.0",
        "babel-preset-react": "^6.24.1",
        "babel-register": "^6.26.0",
        "chai": "^3.5.0",
        "chai-jquery": "^2.0.0",
        "css-loader": "^0.28.11",
        "enzyme": "^3.3.0",
        "jquery": "^2.2.1",
        "jsdom": "^8.1.0",
        "mocha": "^3.5.3",
        "react-hot-loader": "^4.1.3",
        "react-test-renderer": "^16.3.2",
        "style-loader": "^0.21.0",
        "webpack": "^3.4.1",
        "webpack-dev-server": "^2.9.7"
      },
      "dependencies": {
        "antd": "^3.5.1",
        "axios": "^0.18.0",
        "babel-plugin-transform-async-to-generator": "^6.24.1",
        "babel-polyfill": "^6.26.0",
        "babel-preset-stage-1": "^6.1.18",
        "babel-runtime": "^6.26.0",
        "bootstrap": "^4.1.1",
        "core-js": "^2.5.6",
        "lodash": "^3.10.1",
        "react": "^16.3.2",
        "react-dom": "^16.3.2",
        "react-dropzone": "^4.2.9",
        "react-redux": "^5.0.7",
        "react-router": "^2.0.1",
        "react-router-dom": "^4.2.2",
        "reactstrap": "^6.0.1",
        "redux": "4.0.0",
        "redux-form": "^7.3.0",
        "redux-promise": "^0.5.3"
      }
    }

.babelrc

{
  "presets": ["react",[
    "env",
    {
      "modules": false,
      "targets": {
        "node": "current"
      }
    }
  ], "stage-1"
],
"env": {
  "test": {
    "presets": ["react",
      [
        "env",
        {
          "modules": false,
          "debug": true
        }
      ],
      "stage-1"
    ]
  }
 },
  "plugins": [
    ["import",
      { "libraryName": "antd",  "libraryDirectory": "es", "style": "css" }],
      "transform-decorators-legacy",
        "transform-class-properties",
        "transform-async-to-generator",
        "transform-runtime"
  ]
}

webpack.config

module.exports = {
  entry: ['./src/index.js', 'babel-register'],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      },
      {
        test: /\.css$/,
        use: [ 'style-loader', 'css-loader' ]
      }
    ],
  },
  resolve: {
    extensions: ['*', '.js', '.jsx', '.css']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './',
    watchOptions: {
      aggregateTimeout: 300,
      poll: 1000
    }
  },

};

testhelper.js

import _$ from 'jquery';
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-dom/test-utils';
import jsdom from 'jsdom';
import chai, { expect } from 'chai';
import chaiJquery from 'chai-jquery';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import reducers from '../src/reducers';

global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = global.document.defaultView;
global.navigator = global.window.navigator;
const $ = _$(window);

chaiJquery(chai, chai.util, $);

function renderComponent(ComponentClass, props = {}, state = {}) {
  const componentInstance =  TestUtils.renderIntoDocument(
    <Provider store={createStore(reducers, state)}>
      <ComponentClass {...props} />
    </Provider>
  );

  return $(ReactDOM.findDOMNode(componentInstance));
}

$.fn.simulate = function(eventName, value) {
  if (value) {
    this.val(value);
  }
  TestUtils.Simulate[eventName](this[0]);
};

export {renderComponent, expect};
0

There are 0 answers