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

tutorial

#1

Following this https://docs.zeppelinos.org/docs/testing.html

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

ZWeb3.initialize('http://localhost:8545');

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

require('chai').should();

contract('Key', async () => {
  describe('Write information to ledger', () => {

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

    describe('Node Ethereum Address', function() {
      it('Valid Node Ethereum Address', async () => {
        const proxy = await this.project.createProxy(Key);
...

I Get this error:

Using network 'development'.

TypeError: Cannot read property 'Contract' of undefined
    at Function.contract (/home/emiller/src/olypsis/block-n-key/smart-contracts/node_modules/zos-lib/src/artifacts/ZWeb3.ts:57:28)
    at Object.createZosContract (/home/emiller/src/olypsis/block-n-key/smart-contracts/node_modules/zos-lib/src/artifacts/Contract.ts:106:26)
    at Function._getFromPath (/home/emiller/src/olypsis/block-n-key/smart-contracts/node_modules/zos-lib/src/artifacts/Contracts.ts:98:12)
    at Function.getFromLocal (/home/emiller/src/olypsis/block-n-key/smart-contracts/node_modules/zos-lib/src/artifacts/Contracts.ts:53:22)
    at Object.<anonymous> (/home/emiller/src/olypsis/block-n-key/smart-contracts/test/test_Key.js:6:23)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at loader (/home/emiller/src/olypsis/block-n-key/smart-contracts/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/home/emiller/src/olypsis/block-n-key/smart-contracts/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at /home/emiller/.local/share/yarn/global/node_modules/mocha/lib/mocha.js:231:27
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/home/emiller/.local/share/yarn/global/node_modules/mocha/lib/mocha.js:228:14)
    at Mocha.run (/home/emiller/.local/share/yarn/global/node_modules/mocha/lib/mocha.js:536:10)
    at /home/emiller/.local/share/yarn/global/node_modules/truffle/build/webpack:/packages/truffle-core/lib/test.js:118:1
    at process._tickCallback (internal/process/next_tick.js:68:7)
Truffle v5.0.2 (core: 5.0.2)
Node v10.13.0

#2

Hey @Emiller88! This isn’t something I’ve seen before so I’m going to tag @jcarpanelli to see if he knows what’s going on.


#3

Hi @Emiller88! This seems to be a zos-lib#ZWeb3 bug when sending an url as argument to ZWeb3#initialize.
You have two ways to get rid of this issue:

  • Use the default web3.currentProvider that truffle initializes when you run truffle test:
const { TestHelper } = require('zos');
const { Contracts, ZWeb3 } = require('zos-lib');

ZWeb3.initialize(web3.currentProvider);

...
  • Set the HttpProvider manually:
const { TestHelper } = require('zos');
const { Contracts, ZWeb3 } = require('zos-lib');
const Web3 = require('web3');

const httpProviderUrl = 'http://localhost:8545';
const provider = new Web3.providers.HttpProvider(httpProviderUrl);
ZWeb3.initialize(provider);

...

Also, please check that you are using the latest version of both zos and zos-lib, or otherwise run: npm i zos@2.2.1 zos-lib@2.2.1.

I’ve just addressed this issue here.


#4

@jcarpanelli Thanks that worked! I was on zos-lib 2.2.0 but that was not the issue. Your http snippet works. However I do have to follow this work around also. Small testing example throwing error in `this.project`


#5

Great that it worked for you! We’ve just released a patch (v2.2.2) fixing this issue among others :slight_smile: