Web3 ignores Test Helpers configuration for Test Environment

:bomb: Problem
Recently I have switched from the Truffle test-environment to the OpenZeppeling test-environment and I am extremely enjoying the features. However, something confuses me. At the top of my test code I write:

    require('@openzeppelin/test-helpers/configure')({
        provider: 'http://localhost:8545',
        defaultGasPrice: 0
    });

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

    const {
      BN,           // Big Number support
      constants,    // Common constants, like the zero address and largest integers
      expectEvent,  // Assertions for emitted events
      expectRevert, // Assertions for transactions that should fail
    } = require('@openzeppelin/test-helpers');

    const balance = require('@openzeppelin/test-helpers/src/balance');
    const { assert } = require('chai');
    require('chai').should();
    const Escrow = contract.fromArtifact('Escrow');

After this configuration I start the test:

    describe('Escrow', () => {

        const [lawyer, payer, payee] = accounts;

        const amount = 100;

        beforeEach(async () => {

            this.escrow = await Escrow.new(payer, payee, amount);

        });

        it('deposit amount correctly', async () => {

            await this.escrow.deposit({ from: payer, value: amount });
        
            let allAccounts = await web3.eth.getAccounts();
            const gasPrice = await web3.eth.getGasPrice();

            console.log(allAccounts);
            console.log(accounts);
            console.log(gasPrice);

        });

    });

The only thing that doesn’t work is the web3 object. When I log the gasPrice it prints 20000000000 even though I have set it to 0 and the variable allAccounts stores 11 addresses and the accounts variable stores 10. I think I initialize web3 incorrectly but I am not sure what I am doing wrong. I think it is something extremely obvious.

Does someone spot the mistake?

:computer: Environment

        "@nomiclabs/hardhat-web3": "^2.0.0",
        "@openzeppelin/test-helpers": "^0.5.9",
        "web3": "^1.3.0"
        "@openzeppelin/test-environment": "^0.1.8",
        "chai": "^4.2.0",
        "mocha": "^8.2.1"
1 Like

Hi @golden-idea,

Welcome to the community :wave:

OpenZeppelin Test Environment has its own configuration file so I recommend moving your defaultGasPrice to test-environment.config.js

// test-environment.config.js

module.exports = {
 
    node: { // Options passed directly to Ganache client
      gasPrice: 0 // Sets the default gas price for transactions if not otherwise specified.
    },
  };

You can then remove the following from your test. OpenZeppelin Test Environment spins up its own ganache powered blockchain.

    require('@openzeppelin/test-helpers/configure')({
        provider: 'http://localhost:8545',
        defaultGasPrice: 0
    });

OpenZeppelin Test Environment has a defaultSender

A special account that is used by contracts created via contract when no account is specified for a transaction (i.e. there is no explicit from ). This account is not included in accounts to prevent accidental bugs during testing

Which is why you see 11 addresses, so the defaultSender + 10 accounts.

You could add balance to the following require instead.

const {
  BN,           // Big Number support
  constants,    // Common constants, like the zero address and largest integers
  expectEvent,  // Assertions for emitted events
  expectRevert, // Assertions for transactions that should fail
  balance, // Helpers to inspect Ether balances of a specific account.
} = require('@openzeppelin/test-helpers');

1 Like

Perfect! Everything is working now :partying_face:. Thank you very much for your time and help :star2:.

1 Like