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:

2 Likes
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

1 Like
#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!

2 Likes
#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:

1 Like
#7

Hey @Dennison just saw this response. Thanks for checking in. For context, I did have Babel configured and installed to handle ES6. I am planning to put together a guide for setting up the testing environment for our team, and I’ll be sure to include reminders such as babel configuration for ES6. :+1:

#8

Interesting! I would love to take a look at your testing guide when you’re finished. :slight_smile:

I’m actually working on a testing tutorial right now, as lots of people have been asking me about this, so when it’s finished I’ll share it with you to get your feedback. I’d be curious- did you figure out what the source of problem actually was? I would be curious to add it as a watch out for other developers as well.

cheers!

dennison

#9

Hello @pllearns I was curious to see if you had gotten a chance to put together a guide?

1 Like
#10

@Dennison sorry for the radio silence. We’ve been swamped. I should have a guide next week. Also, in more exciting news, today we are in the mainnet with our governance, block producer, and staking contracts from our Simple Consensus Smart Contract library!! Thanks @spalladino, @IvanTheGreatDev , @martriay and you for all of the help and great documentation!

2 Likes
#11

Congrats on the release! That’s awesome :slight_smile:

Don’t hesitate in creating a thread explaining what it is and how did you do it, so the rest of the community can know, ask and learn more about it :zap:

1 Like
#12

Good morning @martriay (and everybody) I wanted to send you the repo in advance of the guide being available. https://github.com/XYOracleNetwork/dapp-scsc-solidity

#13

Awesome job! Mind making a thread out of it so we can include it among our #general:guides-and-tutorials?

1 Like
#14

Yep, I can do that soon. Thanks!

1 Like