Mocha: Mocha error while running Ethereum Smart Contracts tests.

Created on 11 Mar 2018  Β·  11Comments  Β·  Source: mochajs/mocha

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

[email protected] 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! [email protected] test: mocha
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! /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:

[email protected] 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! [email protected] test: mocha
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! /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.

integration needs-feedback question

Most helpful comment

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.

All 11 comments

Your code wrapped with { } at line 1 and last line? You should it.

@CPerezz can you format your message well in order for us to comprehend your question better.
Anyways, this error means that you must write "use strict"; at the top of the file to use block-scope declarations (let and const).

"use strict";
function runs() {
    let blockScopeDecl = "No errors here";
}

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.

I used small 'I' every where for inbox still I am not able to run test
I am using [email protected]
I also tried [email protected] but still got same error
screen shot 2019-02-17 at 1 57 18 pm

@sushanth3366, you misspelled describe...

bro still its showing this error

On Sun, Feb 17, 2019 at 2:48 PM P. Roebuck notifications@github.com wrote:

@sushanth3366 https://github.com/sushanth3366, you misspelled describe
...

β€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/mochajs/mocha/issues/3279#issuecomment-464432747, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AssV5s368QRKVyVwilLmRJbFEx3So_nnks5vOR5vgaJpZM4Sl2Ud
.

screen shot 2019-02-17 at 4 30 01 pm
screen shot 2019-02-17 at 4 23 58 pm
any one please resolve this issue its a bit urjent for me.

I'll use this standard project layout.

$ PROJ="myproj"
$ mkdir ${PROJ}
$ cd ${PROJ}
$ mkdir lib test

Put your "Car" class in its own file...

$ cat << EOF >> "./lib/car.js"
class Car {
  park() {
    return 'stopped';
  }
  drive() {
    return 'vroom';
  }
}

module.exports = Car;

EOF

Now create its test specification...

$ cat << EOF >> "./test/car.spec.js"
const assert = require('assert');
const Car = require('../lib/car');

describe('Car', () => {
  it('can park', () => {
    const car = new Car();
    assert.equal(car.park(), 'stopped');
  });

  it('can drive', () => {
    const car = new Car();
    assert.equal(car.drive(), 'vroom');
  });
});

EOF

Create "package.json" and patch by hand...

$ npm init -y
$ npm install mocha --save-dev
$ "${EDITOR:=pico}" package.json

It should look like this:

{
  "name": "myproj",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "directories": {
    "lib": "lib",
    "test": "test"
  },
  "scripts": {
    "test": "mocha"
  },
  "keywords": [],
  "devDependencies": {
    "mocha": "^5.2.0"
  },
  "engines": {
    "node": ">=6"
  },
  "author": "P. Roebuck <[email protected]>",
  "license": "ISC"
}

Finally, run the test.

$ npm test

> [email protected] test /tmp/myproj
> mocha

  Car
    βœ“ can park
    βœ“ can drive

  2 passing (9ms)

Now (re)add the Etherium-related modules.
Ensure you have build tools for your platform (e.g., Xcode on macOS) installed first*

$ npm install ganache-cli --save
$ npm install web3 --save    # Install requires build tools

Create another test specification to verify your Web3 setup.

$ cat << EOF >> "./test/web3.spec.js"
const assert = require('assert');

describe('Web3', function() {
  this.retries(3);

  it("should load 'ganache' provider", function() {
    const ganache = require('ganache-cli');
    assert.ok(ganache);
  });

  it("should load constructor", function() {
    const Web3 = require('web3');
    assert.ok(Web3);
  });

  it('should create an instance', function() {
    const ganache = require('ganache-cli');
    const Web3 = require('web3');
    const web3 = new Web3(ganache.provider());
    //console.log(web3);
    assert.ok(web3);
  });
});

EOF

Test again.

$ npm test

> [email protected] test /tmp/myproj
> mocha

  Car
    βœ“ can park
    βœ“ can drive

  Web3
    βœ“ should load 'ganache' provider
    βœ“ should load constructor
    βœ“ should create an instance (281ms)

  5 passing (6s)

Man thank u so muchπŸ˜€πŸ˜€!!!

On Mon, 18 Feb 2019 at 12:54 AM, P. Roebuck notifications@github.com
wrote:

Now (re)add the Etherium related modules.
Ensure you have native build tools (e.g., Xcode on macOS) installed first
*

$ npm install ganache-cli --save
$ npm install web3 --save # Install requires build tools

Create another test specification to verify your Web3 setup.

$ cat << EOF >> "./test/web3.spec.js"
const assert = require('assert');

describe('Web3', () => {
let ganache;
let Web3;

before(() => {
ganache = require('ganache-cli');
Web3 = require('web3');
});

it('should create an instance', () => {
const web3 = new Web3(ganache.provider());
console.log(web3);
assert.ok(web3);
});
});

EOF

Test again.

$ npm test

β€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/mochajs/mocha/issues/3279#issuecomment-464497853, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AssV5qGJbh45OPn4ceNKH1ZuXmnWs8aAks5vOaxsgaJpZM4Sl2Ud
.

having a similar issue. When I run test I get an error. It will not retrieve account from ganache

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());

let accounts;

beforeEach(async () => {
// Get list of all accounts
accounts = await web3.eth.getAccounts()

  //Use one of those accounts to deploy
  //the contract

});

descirbe('Inbox', () => {
it('deploys a contract', () => {
console.log(accounts);
});
});

Annotation 2020-01-17 114307

Was this page helpful?
0 / 5 - 0 ratings