Mocha: Switching from --compilers js:babel-core/register to --require babel-core/register throws SyntaxError

Created on 19 Feb 2018  Â·  4Comments  Â·  Source: mochajs/mocha

Prerequisites

  • [x] Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • [x] Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • [x] 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • [x] Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.

Description

With this script in npm test: (before upgrading mocha)

cross-env NODE_ENV=test mocha --reporter=min --no-colors --compilers js:babel-core/register --require babel-polyfill --require ignore-styles --require "test/helper.js" "tools/**/*.spec.js" "src/**/*.spec.js"

Everything works fine, the code is transpiled correctly and tested.

With this code in npm test: (after upgrading mocha to 5*)

cross-env NODE_ENV=test mocha --reporter=min --no-colors --require babel-core/register --require babel-polyfill --require ignore-styles --require "test/helper.js" "tools/**/*.spec.js" "src/**/*.spec.js"

I get this error:

âš¡ user@machine î‚° /project/path î‚° î‚  speed-build î‚° npm test

> [email protected] test /project/path
> cross-env NODE_ENV=test mocha --reporter=min --no-colors --require babel-core/register --require babel-polyfill --require ignore-styles --require "test/helper.js" "tools/**/*.spec.js" "src/**/*.spec.js"

/project/path/node_modules/fetch-mock/src/lib/fetch-handler.js:57
FetchMock.generateResponse = async function (response, url, opts) {
                                  ^^^^^^^^

SyntaxError: Unexpected token function
   at createScript (vm.js:56:10)
   at Object.runInThisContext (vm.js:97:10)
   at Module._compile (module.js:542:28)
   at Module._extensions..js (module.js:579:10)
   at Object.require.extensions.(anonymous function) [as .js] (/project/path/node_modules/babel-register/lib/node.js:152:7)
   at Module.load (module.js:487:32)
   at tryModuleLoad (module.js:446:12)
   at Function.Module._load (module.js:438:3)
   at Module.require (module.js:497:17)
   at require (internal/module.js:20:19)
   at Object.<anonymous> (/project/path/node_modules/fetch-mock/src/lib/index.js:2:22)
   at Module._compile (module.js:570:32)
   at Module._extensions..js (module.js:579:10)
   at Object.require.extensions.(anonymous function) [as .js] (/project/path/node_modules/babel-register/lib/node.js:152:7)
   at Module.load (module.js:487:32)
   at tryModuleLoad (module.js:446:12)
   at Function.Module._load (module.js:438:3)
   at Module.require (module.js:497:17)
   at require (internal/module.js:20:19)
   at Object.<anonymous> (/project/path/node_modules/fetch-mock/src/server.js:7:19)
   at Module._compile (module.js:570:32)
   at Module._extensions..js (module.js:579:10)
   at Object.require.extensions.(anonymous function) [as .js] (/project/path/node_modules/babel-register/lib/node.js:152:7)
   at Module.load (module.js:487:32)
   at tryModuleLoad (module.js:446:12)
   at Function.Module._load (module.js:438:3)
   at Module.require (module.js:497:17)
   at require (internal/module.js:20:19)
   at Object.<anonymous> (/project/path/src/client/lib/__tests__/parseResponse.spec.js:2:1)
   at Module._compile (module.js:570:32)
   at loader (/project/path/node_modules/babel-register/lib/node.js:144:5)
   at Object.require.extensions.(anonymous function) [as .js] (/project/path/node_modules/babel-register/lib/node.js:154:7)
   at Module.load (module.js:487:32)
   at tryModuleLoad (module.js:446:12)
   at Function.Module._load (module.js:438:3)
   at Module.require (module.js:497:17)
   at require (internal/module.js:20:19)
   at /project/path/node_modules/mocha/lib/mocha.js:231:27
   at Array.forEach (native)
   at Mocha.loadFiles (/project/path/node_modules/mocha/lib/mocha.js:228:14)
   at Mocha.run (/project/path/node_modules/mocha/lib/mocha.js:536:10)
   at Object.<anonymous> (/project/path/node_modules/mocha/bin/_mocha:582:18)
   at Module._compile (module.js:570:32)
   at Object.Module._extensions..js (module.js:579:10)
   at Module.load (module.js:487:32)
   at tryModuleLoad (module.js:446:12)
   at Function.Module._load (module.js:438:3)
   at Module.runMain (module.js:604:10)
   at run (bootstrap_node.js:389:7)
   at startup (bootstrap_node.js:149:9)
   at bootstrap_node.js:504:3
npm ERR! Test failed.  See above for more details.

Steps to Reproduce

Upgrade mocha from 3* to 5*, update test scripts as mentioned above.

Expected behavior: [What you expect to happen]
Tests to run as before the upgrade

Actual behavior: [What actually happens]
SyntaxError is thrown

Reproduces how often: [What percentage of the time does it reproduce?]
Every time after upgrade to 5*

Versions

âš¡ user@machine î‚° /project/path î‚° î‚  speed-build î‚° node node_modules/.bin/mocha --version
5.0.1
âš¡ user@machine î‚° /project/path î‚° î‚  speed-build î‚° mocha --version
5.0.1
âš¡ user@machine î‚° /project/path î‚° î‚  speed-build î‚° node --version
v6.11.2
  • Ubuntu Bash on Windows 10
question unconfirmed-bug

Most helpful comment

@phun-ky I have just tested this and it is working fine.

  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-stage-0": "^6.24.1",
    "babel-register": "^6.26.0",
    "mocha": "^5.0.1"
  },
  "scripts": {
    "test": "mocha --reporter=min --no-colors --require babel-core/register --require babel-polyfill index.js"
  },

I believe your issue is with babel itself, are you sure that the .babelrc file is in the root directory of the project? and that it is transpiling async functions

here is my .babelrc for example. and it is transpiling stage 0 syntax in my code

{
  "presets": [
    [
      "env",
      {
        "targets": {
          "node": "current"
        }
      }
    ],
    "stage-0"
  ]
}

All 4 comments

@phun-ky I have just tested this and it is working fine.

  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-stage-0": "^6.24.1",
    "babel-register": "^6.26.0",
    "mocha": "^5.0.1"
  },
  "scripts": {
    "test": "mocha --reporter=min --no-colors --require babel-core/register --require babel-polyfill index.js"
  },

I believe your issue is with babel itself, are you sure that the .babelrc file is in the root directory of the project? and that it is transpiling async functions

here is my .babelrc for example. and it is transpiling stage 0 syntax in my code

{
  "presets": [
    [
      "env",
      {
        "targets": {
          "node": "current"
        }
      }
    ],
    "stage-0"
  ]
}

This is my .babelrc-file, unchanged during the upgrade:

{
  "presets": ["env", "stage-0", "react"],
  "env": {
    "development": {
      "plugins": [
        [
          "react-transform",
          {
            "transforms": [
              {
                "transform": "react-transform-hmr",
                "imports": ["react"],
                "locals": ["module"]
              },
              {
                "transform": "react-transform-catch-errors",
                "imports": ["react", "redbox-react"]
              }
            ]
          }
        ]
      ]
    },
    "test": {
      "plugins": [
        ["babel-plugin-webpack-alias", {
          "config": "./cfg/client/test.js",
          "en": "./src/languages/en.json",
          "fi": "./src/languages/fi.json",
          "dk": "./src/languages/dk.json",
          "no": "./src/languages/no.json",
          "se": "./src/languages/se.json",
        }]
      ]
    }
  }
}

And this is my babel deps in devDeps:

"devDependencies": {
  "babel-cli": "^6.18.0",
  "babel-core": "^6.25.0",
  "babel-eslint": "^7.2.3",
  "babel-loader": "^7.0.0",
  "babel-plugin-add-module-exports": "^0.2.1",
  "babel-plugin-lodash": "^3.2.11",
  "babel-plugin-module-resolver": "^2.5.0",
  "babel-plugin-react-transform": "^2.0.2",
  "babel-plugin-transform-decorators-legacy": "^1.3.4",
  "babel-plugin-transform-es3-member-expression-literals": "^6.22.0",
  "babel-plugin-transform-es3-property-literals": "^6.22.0",
  "babel-plugin-transform-runtime": "^6.23.0",
  "babel-plugin-webpack-alias": "^2.1.2",
  "babel-preset-env": "^1.6.0",
  "babel-preset-es2015": "^6.24.1",
  "babel-preset-es2015-loose": "^8.0.0",
  "babel-preset-react": "^6.3.13",
  "babel-preset-stage-0": "^6.5.0",
  "babel-register": "^6.22.0"
}

Is it correct of me to assume that the previous method with --compilers js:babel-core/register read the .babelrc-file correctly? If so, shouldn't it also be read by --require babel-core/register?

yes the babelrc file is read normally. can you try to change the .babelrc file to the following

{
  "presets": [["env", {
      "development": {
        "plugins": [
          [
            "react-transform",
            {
              "transforms": [
                {
                  "transform": "react-transform-hmr",
                  "imports": ["react"],
                  "locals": ["module"]
                },
                {
                  "transform": "react-transform-catch-errors",
                  "imports": ["react", "redbox-react"]
                }
              ]
            }
          ]
        ]
      },
      "test": {
        "plugins": [
          ["babel-plugin-webpack-alias", {
            "config": "./cfg/client/test.js",
            "en": "./src/languages/en.json",
            "fi": "./src/languages/fi.json",
            "dk": "./src/languages/dk.json",
            "no": "./src/languages/no.json",
            "se": "./src/languages/se.json",
          }]
        ]
      }
    }
  }], "stage-0", "react"]
}

since you are using the env preset. I have ran a small project with the configs in my previous comments (using babel-env preset and stage-0 preset), and the following test:

const box = {
  weight: 2,
  getWeight() { return this.weight; },
};

const { getWeight } = box;

console.log(box.getWeight());

const bigBox = { weight: 10 };
console.log(bigBox::getWeight());

function add(val) { return this + val; }

describe("Some Test", function() {
  it('Logging Test', function() {
    console.log(bigBox::getWeight()::add(5));
  })
})

everything is working fine, i believe something specific in you environment is causing this issue. can you make sure you do not have mocha installed globally, and instead save it as a dev dependency?

Hm, after fiddling with some other stuff, the tests are running as expected, and I can't figure out what I did.. My .babelrc file is unchanged, but if I change it to your suggested syntax, I get "syntaxerror" on import. Closing this issue, since the rest is babel-stuff. Thanks for the patience

Was this page helpful?
0 / 5 - 0 ratings