Pass a value generated in Mocha-before to express app

Issue

Im working with express and mocha for test.
From Express Im getting info from an smart contract deployed in ropsten. So the address is always the same. I have that address in a config file.

For local testing, I want to use testrpc and deploy the smart contract before each test. So when I need to pass the address of the deploy to the express app.

My code is:

Test.js

beforeEach(async function () {
    index = DeployContract() //this returns a random address 
    server = await app.listen(3000)
})

In the app

const CONFIG = require('../config.json')
const contex =  {
  indexAddress: CONFIG.indexAddress, // or .env
  gasMargin: CONFIG.gasMargin,
  web3: web3
}

router.get('/manager', (req, res, next) => {
  const manager = new Manager(contex) //this must be the address returned beforeEach
  // do something
  res.send(200)
})

I need need to use the genereated address for indexAddress: CONFIG.indexAddress, so I can use the contex in the constructor.

Solution

I think you should reference to a global variable where you will save the address generated by DeployContract(), some example would be:

file: contract address

let address = "" 
export default address

then in your beforeEach hook you should import this variable, and set the address to it.
And in your app you should aim to this variable

Answered By – Pablo Alice

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published