4

I've Installed Node@10.x.x, Mocha@6.x.x & Babel@7.x.x. I'm trying to run some mocha tests in ES6 standard with running npm test but I get the following error:

> test@1.0.0 test G:\Ebooks\16 [Test]\test2
> mocha --require @babel/register
G:\Ebooks\16 [Test]\test2\test\index.spec.js:1

    (function (exports, require, module, __filename, __dirname) { import { expect } 
from "chai";
SyntaxError: Unexpected token {
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Module._compile (G:\Ebooks\16 [Test]\test2\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.newLoader [as .js] (G:\Ebooks\16 [Test]\test2\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\mocha.js:330:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\mocha.js:327:14)
at Mocha.run (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\mocha.js:804:10)
at Object.exports.singleRun (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\cli\run-helpers.js:207:16)
at exports.runMocha (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\cli\run-helpers.js:300:13)
at Object.exports.handler.argv [as handler] (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\cli\run.js:296:3)
at Object.runCommand (G:\Ebooks\16 [Test]\test2\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (G:\Ebooks\16 [Test]\test2\node_modules\yargs\yargs.js:1104:24)
at Object.parse (G:\Ebooks\16 [Test]\test2\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\cli\cli.js:63:6)
at Object.<anonymous> (G:\Ebooks\16 [Test]\test2\node_modules\mocha\bin\_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
npm ERR! Test failed.  See above for more details.

It seems that Babel does not convert ES6 files. My package.json file is:

{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
    "test": "./node_modules/.bin/mocha ---require @babel/register"
},
"devDependencies": {
    "@babel/cli": "^7.4.3",
    "@babel/core": "^7.4.3",
    "@babel/node": "^7.2.2",
    "@babel/preset-env": "^7.4.3",
    "@babel/register": "^7.4.0",
    "chai": "^4.2.0",
    "mocha": "^6.1.0",
    "nodemon": "^1.18.10"
}
}

and my .babelrc is:

{
"presets": ["@babel/preset-env"]

}

Can anyone help me with this error?

test file is ./test/index.spec.js:

import { expect } from "chai"
import sayHello from "../src/index"

describe("index test", () => {
describe("sayHello function", () => {
    it("should say Hello guys!", () => {

        const str = sayHello();
        expect(str).to.equal("Hello guys!")
    })
})
})

& .src/index.js is:

const sayHello = _ => "Hello guys!"

console.log(sayHello())

export default sayHello

This is the link of tutorial i followed it's instruction: https://dev.to/bnorbertjs/my-nodejs-setup-mocha--chai-babel7-es6-43ei

i changed the package.json a little because --compiler is deprecated and does not work anymore.

Ehsan Shekari
  • 876
  • 2
  • 10
  • 19
  • Can you provide a sample test case say your `index.spec.js`? – josephting Apr 08 '19 at 08:03
  • 1
    i updated the post. now it contains all files and the link to the tutorial i followed its instructions. – Ehsan Shekari Apr 08 '19 at 10:37
  • 1
    It worked fine on my mac actually. How about doing a clean `npm install` and try again? Delete your `node_modules` directory, do `npm install` and try again. – josephting Apr 09 '19 at 00:26
  • I reinstalled node modules but i still have the same error. my os is windows 7. – Ehsan Shekari Apr 09 '19 at 18:00
  • 1
    Unfortunately, I don't have windows 7 machine but it worked on my windows 10 wsl too. If reinstalling node_modules didn't work, I would consider giving [yarn](https://yarnpkg.com) a try. – josephting Apr 10 '19 at 01:36
  • Thank you. I will try it on ubuntu 18.04 to see what will happen. – Ehsan Shekari Apr 10 '19 at 03:31
  • 1
    Thank you josephting. I tried to run the example on ubuntu and everything was right. – Ehsan Shekari Apr 10 '19 at 08:57
  • Does this answer your question? [Running Mocha 6 ES6 tests with Babel 7, how to set up?](https://stackoverflow.com/questions/56682958/running-mocha-6-es6-tests-with-babel-7-how-to-set-up) – vsync Dec 25 '22 at 14:23

2 Answers2

2

The following may be useful to others who tried the solution in the linked tutorial, but still had the same issue. I also needed to install npm install --save-dev @babel/plugin-transform-runtime and npm install --save @babel/runtime

Then, I needed to make the following addition to my .babelrc file

plugins: [ '@babel/plugin-transform-runtime' ]

Oisín Foley
  • 2,317
  • 2
  • 22
  • 29
  • Tried what's listed in the link plus the additional installs and addition to the `plugins: []` section of my .babelrc file. Still CANNOT get mocha to work. I either end up with `cannot find module 'vscode'` or `cannot find module myCustomModule' even after requiring it. – trebleCode Aug 22 '19 at 20:18
2

Try this Test example:

index.js

const changeStr = (str) => {
    return str
}
export default changeStr;

test\index.spec.js

import { expect } from 'chai';
import changeStr from '../index';

describe('#changeStr', () => {
  it('should not change empty string', () => {
    expect(changeStr('')).to.equal('');
  });
});

.babelrc

{
    "presets": ["@babel/preset-env"]
}

.mocharc.yml

require: '@babel/register'

package.json

{
  "name": "tdd",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "mocha"
  },
  "license": "MIT",
  "devDependencies": {
    "@babel/cli": "^7.10.5",
    "@babel/core": "^7.11.1",
    "@babel/preset-env": "^7.11.0",
    "@babel/register": "^7.10.5",
    "chai": "^4.2.0",
    "mocha": "^8.1.1"
  }
}

RUN

yarn test

or

yarn mocha
Violeta
  • 21
  • 1
  • 2