0

I had a few problems with testing smart contracts that seemed perfectly fine to me. I have reduced the repository further and further and am left with this.
MappingContract.sol

pragma solidity = 0.8.11;

contract MappingContract{
    mapping(uint256 => uint256) internal identity;

    function addNumber(uint256 number) external{
        identity[number] = number;
    }

    function getIdentity(uint256 number) external returns (uint256 numberOut){
        numberOut = identity[number];
    }

    constructor() {
        identity[0] = 0;
    }
}

testMapping.spec.ts

const { waffle } = require('hardhat');
const { ethers } = require('hardhat');
const { expect } = require('chai');

const { Contract, Wallet, BigNumber } = ethers;
const { MockProvider, deployContract } = waffle;

const overrides = {
    gasLimit: 13000000
};

describe('MappingTests', () => {

    it('test', async () => {
        const signers = await ethers.getSigners();
        let owner = signers[0];

        let mAddress= await ethers.getContractFactory("MappingContract");

        let m = await mAddress.deploy();

        let result = await m.getIdentity(0);
        
    });
});

When I run the test, I get the following error

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (./test/testMapping.spec.ts)
      at listOnTimeout (internal/timers.js:554:17)
      at processTimers (internal/timers.js:497:7)

The contract and the tests seem fine to me and it works when run with remix but fails when run with hardhat. Am I using a wrong version of hardhat or any other package? I had similar issues with truffle. Here are my configuration files
package.json

{
  "name": "MappingTest",
  "version": "1.1.0-beta.0",
  "description": "Mapping Tests",
  "engines": {
    "node": ">=10"
  },
  "files": [
    "build",
    "contracts"
  ],
  "dependencies": {},
  "devDependencies": {
    "@nomiclabs/hardhat-ethers": "^2.0.4",
    "@nomiclabs/hardhat-waffle": "^2.0.2",
    "chai": "^4.2.0",
    "ethereum-waffle": "^3.4.0",
    "ethers": "^5.5.4",
    "hardhat": "2.8.3",
    "mocha": "^6.2.2",
    "typescript": "^3.7.3"
  },
  "scripts": {
    "compile": "yarn hardhat compile",
    "test": "yarn mocha test/testMapping.spec.ts"
  },
  "license": "GPL-3.0-or-later"
}

hardhat.config.js

require('@nomiclabs/hardhat-ethers');
require('@nomiclabs/hardhat-waffle');

module.exports = {
  networks: {
      hardhat: {
          mining: {
              auto: false,
              interval: 5000,
          },
          blockGasLimit: 13000000,
          gasPrice: 20
      },
  },
  
  solidity: {
      compilers:[
          {
              version: "0.8.11"
          }
      ]
  }
};

1 Answer 1

0

Your hardhat is set to mine a block every 5 seconds (in your hardhat config). Your test seems to timeout after 2 seconds. Obviously these things don't work well together.

I highly recommend you to make the hardhat network to mine blocks automatically, unless you're sure you need it to mine only at intervals. To enable automining, just remove the block mining.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.