Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not Use Node, Mocha & [email protected] Together

I've Installed [email protected], [email protected] & [email protected]. I'm trying to run some mocha tests in ES6 standard with running npm test but I get the following error:

> [email protected] 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.

like image 733
Ehsan Shekari Avatar asked Oct 22 '25 14:10

Ehsan Shekari


1 Answers

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' ]

like image 199
Oisín Foley Avatar answered Oct 25 '25 05:10

Oisín Foley