0

So as the title suggested i tested a smart contract (ERC721) but the the test wont pass. I am using hardhat environment for testing, that is hardhat-waffle and chai library. Here is the smart contract and the testing script

// SPDX-License-Identifier: MIT
    pragma solidity ^0.8.4;

    import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
    import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
    import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
    import "@openzeppelin/contracts/access/Ownable.sol";
    import "@openzeppelin/contracts/utils/Counters.sol";

contract Depression is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable {
using Counters for Counters.Counter;

Counters.Counter private _tokenIdCounter;

constructor() ERC721("Depression", "DEP") {}

function safeMint(address to, string memory uri) public onlyOwner {
    uint256 tokenId = _tokenIdCounter.current();
    _tokenIdCounter.increment();
    _safeMint(to, tokenId);
    _setTokenURI(tokenId, uri);
}

// The following functions are overrides required by Solidity.

function _beforeTokenTransfer(address from, address to, uint256 tokenId)
    internal
    override(ERC721, ERC721Enumerable)
{
    super._beforeTokenTransfer(from, to, tokenId);
}

function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
    super._burn(tokenId);
}

function tokenURI(uint256 tokenId)
    public
    view
    override(ERC721, ERC721URIStorage)
    returns (string memory)
{
    return super.tokenURI(tokenId);
}

function supportsInterface(bytes4 interfaceId)
    public
    view
    override(ERC721, ERC721Enumerable)
    returns (bool)
{
    return super.supportsInterface(interfaceId);
}
}

and here is the script for testing

import { ethers } from "hardhat";
import chai from "chai";
import { solidity } from "ethereum-waffle";
import { Depression__factory, Depression } from "../typechain";

chai.use(solidity);
const { expect } = chai;
tokenAddress: string = "0xb962471ea0ff5db1d775374f8af342267326fbdc";

describe("Unit Test", () => {
it("VotingNFTContract", async () => {
try {
  const [deployer] = await ethers.getSigners();

  const tokenInstance = new Depression__factory(deployer).attach(tokenAddress);
  // const toMint = ethers.utils.parseEther("1");
  const toAddress = await deployer.getAddress();
  const beforeAmount = await tokenInstance.balanceOf(toAddress);
  const beforeAmountNumber = beforeAmount.toNumber()
  const tx = await tokenInstance.safeMint(toAddress, "http://localhost/api/token/");
  await tx.wait();
  const afterAmount = await tokenInstance.balanceOf(toAddress);
  const afterAmountNumber = afterAmount.toNumber();
  console.log("zap test", beforeAmount.toNumber(), afterAmount.toNumber());
  expect(beforeAmountNumber).to.lessThan(afterAmountNumber);
} catch (error) {
  console.log("Error VotingNFT Deploy", error);
}
});
});

Heres the result of the test

$ yarn test
yarn run v1.22.5
npx hardhat test
No need to generate any newer typings.

Unit Test
  zap test 29 30
  Error: connect ETIMEDOUT 52.45.222.162:443
  Error: connect ETIMEDOUT 52.45.222.162:443

1) VotingNFTContract
Done in 115.79s.

I already use async await for the asynchronous function like balanceOf, and even already got the values in the console.log that i put, but somehow the expect block wont accept the values and then the test timedout. Any idea what i did wrong here?

1 Answer 1

0

Turns out its because of the gas reporter package that i enabled, when i disable it, the test run successfully.

Your Answer

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

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