0
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const inbox = require('../compile');

const web3 = new Web3(ganache.provider());

const interface = inbox.interface;
const bytecode = inbox.bytecode;

let contractAddress,inboxContract;
beforeEach(()=>{
    // Get a list of all accounts
   return web3.eth.getAccounts()
        .then(accountList=>{
            contractAddress = Array.from(accountList)[0];
            return contractAddress;
        })
        .then(contractAddress=>{
          inboxContract =  new web3.eth.Contract(JSON.parse(interface))
                .deploy({data: bytecode, arguments:['Hi there!']})
                .send({from: contractAddress, gas: '1000000'});
                return inboxContract;
        })
        
        
    //Use one of the accounts to deploy the contract
});

describe('Inbox contract test',()=>{
   
    it('Successfully Deploy Test',()=>{
        assert.ok(inboxContract.options.address);
    })
    it('Default Value test',()=>{
        
    })
    it('setMessage Test',()=>{

    })
})

output- I want beforeEach to execute completely before running it() block. Am I missing something here in Promise. Ideally beforeEach() should complete before executing the test cases.

Screenshot

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 2
    Return the promise: `return web3.eth.getAccounts(/* etc */)` – Mark Jan 08 '19 at 22:09
  • I am returning promise from beforeEach() block but still facing same error. – Sushant Sharma Jan 09 '19 at 18:22
  • Returning the promise should be enough (you don't have any catches - are you sure there are no errors?) but depending on your version of Mocha [this answer could be useful](https://stackoverflow.com/a/24862303/954940) – Adam Jenkins Jan 09 '19 at 19:15
  • My mocha ver is 5.2.0. Below is o/p while running this code-- Inbox contract test (node:13416) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added. Use emitter.setMaxListeners() to increase limit 1) Successfully Deploy Test √ Default Value test √ setMessage Test 2 passing (382ms) 1 failing 1) Inbox contract test Successfully Deploy Test: TypeError: Cannot read property 'address' of undefined at Context.it (test\inbox.test.js:36:41) npm ERR! code ELIFECYCL – Sushant Sharma Jan 09 '19 at 19:23
  • @SushantSharma I guess `deploy` and `send` methods are async right? – deerawan Feb 01 '19 at 23:32

2 Answers2

0

the code for beforeEach should be inside of the describe and then you could use async - await instead of the standard promises which makes for a nicer syntax.

this would look like so

describe('Inbox contract test',()=>{
  const inboxContract =  new web3.eth.Contract(JSON.parse(interface))

  beforeEach(async ()=>{
    // Get a list of all accounts
    const accountList = await web3.eth.getAccounts()
    const contractAddress = Array.from(accountList)[0];
    let receipt = await inboxContract.deploy({data: bytecode, arguments:['Hi there!']})
      .send({from: contractAddress, gas: '1000000'});
    //Use one of the accounts to deploy the contract
    inboxContract.options.address = receipt.contractAddress
  });
...

but you will have to make sure that your tests run inline because the global inboxContract variable will be replaced before each test

Micha Roon
  • 3,957
  • 2
  • 30
  • 48
0

change your beforeEach block with following

beforeEach(() => {
    return web3.eth.getAccounts()
        .then(accountList => {
            return Array.from(accountList)[0];
        })
        .then(account => {
            contractAddress = account;
            return new web3.eth.Contract(JSON.parse(interface))
                .deploy({ data: bytecode, arguments: ['Hi there!'] })
                .send({ from: account, gas: '1000000' });
        })
        .then(contract => {
            inboxContract = contract;
        })
});
Iftifar Taz
  • 799
  • 6
  • 13