OpenZeppelin Test Environment, test failing

Hi everbody,

So far I’ve been following the guides on your webpage for writing Smart contracts, deploying and testing them. But now, my first tests are unsuccessful.

I used the Openzeppelin Test Environment and mocha chai as a tester. I don’t use truffle so I setup a test configuration file in my main project folder that uses web3. I wrote the code for the test and run with npm test. This is the error I receive:

openzepper@1.0.0 test /home/user/Documents/Openzepp1/Openzeppelin1_git
mocha --exit --recursive test

ReferenceError: Web3 is not defined
    at Object.<anonymous> (/home/user/Documents/Openzepp1/Openzeppelin1_git/test/Box.test.js:8:14)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at /home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/mocha/lib/mocha.js:314:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/mocha/lib/mocha.js:311:14)
    at /home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/mocha/lib/mocha.js:347:12
    at new Promise (<anonymous>)
    at Mocha.loadFilesAsync (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/mocha/lib/mocha.js:346:12)
    at singleRun (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/mocha/lib/cli/run-helpers.js:107:15)
    at exports.runMocha (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/mocha/lib/cli/run-helpers.js:144:11)
    at Object.exports.handler (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/mocha/lib/cli/run.js:306:11)
    at Object.runCommand (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/yargs/lib/command.js:242:26)
    at Object.parseArgs [as _parseArgs] (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/yargs/yargs.js:1096:28)
    at Object.parse (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/yargs/yargs.js:575:25)
    at Object.exports.main (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/mocha/lib/cli/cli.js:68:6)
    at Object.<anonymous> (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/mocha/bin/mocha:133:29)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
npm ERR! Test failed.  See above for more details.
user@Ethereum:~/Documents/Openzepp1/Openzeppelin1_git$ 
/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/ganache-core/lib/subproviders/geth_api_double.js:39
        callback(self.initialization_error, self.state);
        ^
TypeError: Cannot read property 'port' of null
    at /home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/@openzeppelin/test-environment/src/ganache-server.ts:36:51
    at Immediate.<anonymous> (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/ganache-core/lib/subproviders/geth_api_double.js:39:9)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)

Seems like it can’t find Web3 and can’t connect to the network. I define Web3 and tell it to use the local ganache provided by the test environment this way:

// test/Box.test.js

// Load dependencies

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

const web3 = new Web3('@openzeppelin/test-environment');
const loader = setupLoader({ provider: web3 }).web3;

I guess this is false. Can anybody point me into the right direction?

1 Like

Hi @tschilpi,

Welcome to the community :wave:

I am sorry that you are having issues with your tests.

I would recommend looking at the OpenZeppelin Learn guides on Writing Automated Smart Contract Tests

To get a web3 instance from the local blockchain in OpenZeppelin Test Environment you can use the following:

const { accounts, contract, web3 } = require('@openzeppelin/test-environment');

See the API: https://docs.openzeppelin.com/test-environment/0.1/api#web3

Hi @abcoathup,

Thanks for your warm welcome.

I’ve been using the API and all the documentation you have but couldn’t figure it out.
The Box.contract works fine on a local ganache address. I can store a value and retrieve it.

This is the test code I’ve been using to test it, it is straight from your ‘‘Writing Automated Smart Contract Tests’’ guide:

// test/Box.test.js

// Load dependencies

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

// Load compiled artifacts
const Box = contract.fromArtifact('Box');

// Start test block
describe('Box', function () {
  const [ owner ] = accounts;

  beforeEach(async function () {
    // Deploy a new Box contract for each test
    this.contract = await Box.new({ from: owner });
  });

  // Test case
  it('retrieve returns a value previously stored', async function () {
    // Store a value - recall that only the owner account can do this!
    await this.contract.store(42, { from: owner });

    // Test if the returned value is the same one
    // Note that we need to use strings to compare the 256 bit integers
    expect((await this.contract.retrieve()).toString()).to.equal('42');
  });
}); 

And this is the error I receive:

> openzepper@1.0.0 test /home/user/Documents/Openzepp1/Openzeppelin1_git
> mocha --exit --recursive test



  Box
    1) "before each" hook for "retrieve returns a value previously stored"


  0 passing (25ms)
  1 failing

  1) Box
       "before each" hook for "retrieve returns a value previously stored":
     TypeError: Box.new is not a function
      at Context.<anonymous> (test/Box.test.js:17:34)



npm ERR! Test failed.  See above for more details.
user@Ethereum:~/Documents/Openzepp1/Openzeppelin1_git/test$ 
/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/ganache-core/lib/subproviders/geth_api_double.js:39
        callback(self.initialization_error, self.state);
        ^
TypeError: Cannot read property 'port' of null
    at /home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/@openzeppelin/test-environment/src/ganache-server.ts:36:51
    at Immediate.<anonymous> (/home/user/Documents/Openzepp1/Openzeppelin1_git/node_modules/ganache-core/lib/subproviders/geth_api_double.js:39:9)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)

I don’t understand why it says that ‘‘Box.new is not a function’’.

1 Like

Hi @tschilpi,

I wasn’t able to reproduce. I can run the test without any issues.

Can you advise the versions that you are using?

I am running on WSL2 on Windows 10, node v10.19.0, npm 6.14.5, OpenZeppelin CLI 2.8.2.

Hi @abcoathup

I am running Ubuntu 18.04.4 on a Ethereum Virtual Machine, with node v10.20.1, npm v6.14.4 and Openzeppelin CLI 2.8.2.

1 Like

Hi @tschilpi,

It sounds like we are on similar environments.

Other than having to install npm i @truffle/debug-utils (https://github.com/OpenZeppelin/openzeppelin-test-environment/issues/105) I didn’t have an issue running the test.

Would you mind creating a new project and trying again.

Test run

$ npm run test

> tschilpi@1.0.0 test /home/abcoathup/projects/forum/tschilpi
> mocha --exit --recursive test



  Box
    ✓ retrieve returns a value previously stored (102ms)


  1 passing (714ms)

Box.sol

// contracts/Box.sol
pragma solidity ^0.5.0;

contract Box {
    uint256 private value;

    // Emitted when the stored value changes
    event ValueChanged(uint256 newValue);

    // Stores a new value in the contract
    function store(uint256 newValue) public {
        value = newValue;
        emit ValueChanged(newValue);
    }

    // Reads the last stored value
    function retrieve() public view returns (uint256) {
        return value;
    }
}

Box.test.js

// test/Box.test.js

// Load dependencies

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

// Load compiled artifacts
const Box = contract.fromArtifact('Box');

// Start test block
describe('Box', function () {
  const [ owner ] = accounts;

  beforeEach(async function () {
    // Deploy a new Box contract for each test
    this.contract = await Box.new({ from: owner });
  });

  // Test case
  it('retrieve returns a value previously stored', async function () {
    // Store a value - recall that only the owner account can do this!
    await this.contract.store(42, { from: owner });

    // Test if the returned value is the same one
    // Note that we need to use strings to compare the 256 bit integers
    expect((await this.contract.retrieve()).toString()).to.equal('42');
  });
});