0

Solidity Contract: index.sol

pragma solidity ^0.5.0;

contract Inbox{
    string public message;

constructor(string memory IntialMessage) public{
    message = IntialMessage;
}

function setMessage(string memory newMessage) public{
    message = newMessage;
}
}

compile.js

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const inboxPath = path.resolve(__dirname,"contracts","index.sol");
const source = fs.readFileSync(inboxPath,"utf8");

var input = {
language: 'Solidity',
sources: {
    'index.sol': {
        content: source
    }
},
settings: {
    outputSelection: {
        '*': {
            '*': [ '*' ]
        }
    }
}
}

const buildPath = path.resolve(__dirname, 'build');
output=JSON.parse(solc.compile(JSON.stringify(input)));

if(output.errors) {
output.errors.forEach(err => {
    console.log(err.formattedMessage);
});
} else {
const contracts = output.contracts["index.sol"];
console.log("Bytecode Of the Contracts:\n");
for (let contractName in contracts) {
    const contract = contracts[contractName];
    module.exports= 
          {"abi":contract.abi,"bytecode":contract.evm.bytecode.object};
    fs.writeFileSync(path.resolve(buildPath, `${contractName}.json`), 
    JSON.stringify(contract.abi, null, 2), 'utf8');
    console.log(contractName+": "+contract.evm.bytecode.object+"\n");
}
}

index.test.js:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');  //Web3 is a constructor of the web3 module

const web3 = new Web3(ganache.provider()); //web3 is an instance of the web3 module
const {abi, bytecode} = require('../compile');
const inboxBuild = require('../build/Inbox.json');
const abiObj = require('solc/abi');

let newABI;
newABI = abiObj.update('0.3.6',inboxBuild);

let accounts;
let inbox;

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

    inbox = await new web3.eth.Contract(newABI)
        .deploy({data:bytecode,arguments:["Hi there!"]})
        .send({from:accounts[0], gas: '1000000'});
});

describe("Inbox",() => {
    it("deploys a contract", () => {
        console.log(inbox);
    });
});

I am getting the following error:

Inbox
    1) "before each" hook for "deploys a contract"


  0 passing (2s)
  1 failing

  1) "before each" hook for "deploys a contract":
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.




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!     C:\Users\Aashutosh Singh\AppData\Roaming\npm-cache\_logs\2019-09-24T04_11_07_715Z-debug.log

Thank you to whoever tries to solve the problem.

1

There is no real problem with your code. Just take a look at the error message. It tells you that you only have 2 seconds time to execute your code. Cause of the nature of blockchain (and ethereum) your deploy will most likely take longer than this.

So how do you fix it?

In you project root folder you have a file called package.json. When running tests you most likely added a "test script" there.

So change from this: "scripts": { "test": "mocha"}

To this: "scripts": { "test": "mocha --timeout 40000" }

This way you now have 40 seconds to run your program. Now you need to execute your test.js like this. (I hope you already did it that way)

npm run test

Where is my abi?

Your index.test.js seems to get the abi in this line:

const {abi, bytecode} = require('../compile');

Now your abi is in the abi variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.