Has anyone used the OpenZeppelin Test Helpers with OZ SDK?

Has anyone used the oz Test Helpers with oz SDK?

1 Like

Hi @pkr,

Why do you ask? Are you having an issue?

Let me know how I can help.

I want to use it, but just not sure where to get started?

1 Like

Hi @pkr,

The README for OpenZeppelin Test Helpers includes a Reference for the different functionality.

As well as using OpenZeppelin Test Helpers I recommend looking at the recently released OpenZeppelin Test Environment:

Please find below a simple Sample upgradeable contract and test. (based on the Sample used in the documentation to show testing an upgradeable contract with Truffle https://docs.openzeppelin.com/sdk/2.6/testing)
I used mocha and chai

Install

npm install @openzeppelin/cli
npm install @openzeppelin/upgrades 
npm install --save-dev @openzeppelin/test-environment
npm install --save-dev @openzeppelin/test-helpers
npm install --save-dev chai 
npm install --save-dev mocha 

Sample.sol

pragma solidity ^0.5.0;

contract Sample {
    function greet() public pure returns (string memory) {
        return "A sample";
    }

    function number() public pure returns (uint256) {
        return 42;
    }
}

Sample.test.js

const { accounts, contract, web3 } = require('@openzeppelin/test-environment');
const { BN, balance, ether } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');

const { TestHelper } = require('@openzeppelin/cli');
const { Contracts, ZWeb3 } = require('@openzeppelin/upgrades');

ZWeb3.initialize(web3.currentProvider);

const Sample = Contracts.getFromLocal('Sample');

describe('Sample', function () {

  beforeEach(async function () {
    this.project = await TestHelper();
    this.proxy = await this.project.createProxy(Sample);
  })

  it('has greet', async function () {
    expect(await this.proxy.methods.greet().call()).to.be.equal('A sample');
  })

  it('has a number', async function () {
    expect(await this.proxy.methods.number().call()).to.be.bignumber.equal('42');
  })
})

Test

Compile the contract

npx oz compile

Run the tests

npx mocha --exit --recursive test

Alternatively add script to package.json and then run npm test

  "scripts": {
    "test": "npx mocha --exit --recursive test"
  }

Test results

$ npm test

> testsdk@1.0.0 test /c/Users/andre/Documents/projects/forum/testsdk
> npx mocha --exit --recursive test



  Sample
    ✓ has greet (40ms)
    ✓ has a number


  2 passing (721ms)
3 Likes

Hi @pkr,

How did you get on? Do you need more information?