mocha: Mocha error while running Ethereum Smart Contracts tests.

Hi all. I’m opening this issue in order to try to fix some annoying problems I’m having while running ethereum smart contracts tests.

Here is my Inbox.test.js: `{ const assert = require(‘assert’); const ganache = require(‘ganache-cli’); const Web3 = require(‘web3’); const web3 = new Web3(ganache.provider()); //const { interface, bytecode } = require(‘…/compile’);

let accounts; let inbox; beforeEach(async () => { accounts = await web3.eth.getAccounts();

inbox = await new web3.eth.Contract(JSON.parse(interface)) .deploy({ data: bytecode, arguments:[‘Hi there!’]}) .send({ from: accounts[0], gas:‘1000000’ }) });

describe(‘Inbox’, ()=>{ it(‘deploys a contract’, () => {

assert.ok(inbox.options.address);

}); }) }`

And here it is my package.json file: { “name”: “inbox”, “version”: “1.0.0”, “description”: “”, “main”: “index.js”, “scripts”: { “test”: “mocha” }, “author”: “”, “license”: “ISC”, “dependencies”: { “ganache-cli”: “^6.1.0”, “loadash”: “^1.0.0”, “mocha”: “^4.0.1”, “node-modules”: “^1.0.1”, “solc”: “^0.4.19”, “web3”: “^1.0.0-beta.26” } }

When I run: npm run test //I get the following console output:

root@XXXXXX:/home/pathtoproject/inbox# npm run test

inbox@1.0.0 test /pathtoproject/inbox mocha

/pathtoproject/inbox/test/Inbox.test.js:6 const { interface, bytecode } = require(‘…/compile’); ^

SyntaxError: Unexpected token { at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:374:25) at Object.Module._extensions…js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Module.require (module.js:354:17) at require (internal/module.js:12:17) at /usr/lib/nodejs/mocha/lib/mocha.js:172:27 at Array.forEach (native) at Mocha.loadFiles (/usr/lib/nodejs/mocha/lib/mocha.js:169:14) at Mocha.run (/usr/lib/nodejs/mocha/lib/mocha.js:356:31) at Object. (/usr/lib/nodejs/mocha/bin/_mocha:366:16) at Module._compile (module.js:410:26) at Object.Module._extensions…js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Function.Module.runMain (module.js:442:10) at startup (node.js:136:18) at node.js:966:3 npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! inbox@1.0.0 test: mocha npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the inbox@1.0.0 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! /root/.npm/_logs/2018-03-11T11_15_11_436Z-debug.log

Then if I try to delete the line that gives problems this happens at running npm run test:

inbox@1.0.0 test /pathtoproject/inbox

mocha

/home/pathtoproject/inbox/test/Inbox.test.js:8 let accounts; ^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outsidestrict mode at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:374:25) at Object.Module._extensions…js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Module.require (module.js:354:17) at require (internal/module.js:12:17) at /usr/lib/nodejs/mocha/lib/mocha.js:172:27 at Array.forEach (native) at Mocha.loadFiles (/usr/lib/nodejs/mocha/lib/mocha.js:169:14) at Mocha.run (/usr/lib/nodejs/mocha/lib/mocha.js:356:31) at Object. (/usr/lib/nodejs/mocha/bin/_mocha:366:16) at Module._compile (module.js:410:26) at Object.Module._extensions…js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Function.Module.runMain (module.js:442:10) at startup (node.js:136:18) at node.js:966:3 npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! inbox@1.0.0 test: mocha npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the inbox@1.0.0 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! /home/XxxxxX/.npm/_logs/2018-03-11T11_35_19_312Z-debug.log

Versioning and other stuff: mocha --version --> 1.20.1 npm --version --> 5.6.0 node --version --> v.9.8.0

So anyone has any idea of what’s happening? I’ve tried to reinstall node and npm, clear npm cache etc and nothing seems to work.

Thanks to all.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (5 by maintainers)

Most upvoted comments

I had the same issue. We are following a Udemy course to learn solidity.

The last line of your compile.js file references the contract names. In our case, the contract is named “Inbox” (with a capital i) Make sure the last line of your compile statement is nameing “Inbox” correctly, with a capital “I” like so: module.exports = solc.compile(source, 1).contracts[':Inbox']; This sould fix your problem.

If inbox it left un-capitalised, it will point the soliditycompiler to the ‘inbox’ contract (which doesn’t exist), instead of the proper “Inbox” contract with a capital I.