Small testing example throwing error in `this.project`


#1

So I did try to implement the TestHelper environment while running a test in this stakableToken proxy test:

const { TestHelper } = require(`zos`)
const { Contracts, ZWeb3 } = require(`zos-lib`)

ZWeb3.initialize(web3.currentProvider)

const stakableToken = Contracts.getFromLocal(`XyStakableToken`)
const ERC20 = artifacts.require(`XyERC20Token.sol`)

require(`chai`).should()

contract(`XyStakableToken`, () => {
  beforeEach(async () => {
    this.project = await TestHelper()
  })

  it(`should create a proxy`, async () => {
    const proxy = await this.project.createProxy(stakableToken)
    const result = await proxy.methods.mint(`0xffcf8fdee72ac11b5c542428b35eef5769c409f0`).call()
    result.should.eq(true)
  })

  it(`should create a proxy for the EVM package`, async () => {
    const proxy = await this.project.createProxy(ERC20, { contractName: `XyStakableToken`, packageName: `openzeppelin-eth` })
    const result = await proxy.methods.mint(`0xffcf8fdee72ac11b5c542428b35eef5769c409f0`).call()
    result.should.eq(true)
  })
})

I continue to get the error in the beforeEach hook, and I think that it may be because TestHelper is undefined? My other unit tests are working, I’m just wondering if I am missing an argument or some step that I didn’t see in the walkthrough. Thanks again!


#2

Nevermind. Solved. Thanks!


#3

Hey pllerans! Glad you figured it out, sorry we didn’t help out earlier.

Would you mind posting your solution so others can see it if they run into a similar problem? :slightly_smiling_face:


Testing upgradable contracts tutorial TypeError: Cannot read property 'Contract' of undefined
#4

Hey @IvanTheGreatDev sorry that I didn’t respond earlier. I’ve been busy with another project.

The change is:

contract(`XyStakableToken`, () => {
  beforeEach(async () => {
    this.project = await TestHelper()
  })

to

contract(`XyStakableToken`, () => {
  let project
  beforeEach(async () => {
    project = await TestHelper()
  })

This solved one problem where this.project was undefined, but now I am working through another error TypeError: Cannot read property 'forEach' of undefined I am still having trouble with the TestHelper. Sorry, I thought I had it all solved.

I’ll add that I am using txParams from an account address that I am using in the TestHelper on each transaction. I also restored the openzeppelin-eth for the ERC20


#5

Update, the tests are starting to work! I can’t identify why exactly, maybe it was adding the openzeppelin-eth to my zos.json file? I’ll try to identify what happened. Thanks!


#6

I’m happy you solved it @pllearns As a note, to anyone else who has this problem:

The origin of the problem here is that you are using arrow functions (ES6 syntax) which is not currently supported without transpiling. These tests won’t run under ES6 unless you have babel installed and configured in your project. This means that:

 beforeEach(async () => {
   this.project = await TestHelper()
 })

In this code snippet “this” actually isn’t working as the arrow function does not bind it’s own this hence, this.project doesn’t work futher on.

You are correct that you can solve it by your solution, but a more reliable long term solution would be to either transpile with Babel to support ES6 Syntax or to replace your arrow functions with regular async function() {} .

So instead it should look like this:

 beforeEach(async function () {
   this.project = await TestHelper()
 })

Keep in mind this advice also extends to the arrow functions located in the it() blocks.

For those who are looking for more context: