Search code examples
ethereumsolidityethers.jshardhat

Error: missing revert data in call exception while testing with Hardhat


I want to test get my function TokenUri using hardhat. Here it is:


    function tokenURI(uint256 tokenId) public view virtual override returns (string memory){
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
        string memory currentBaseURI = _baseURI();
        return bytes(currentBaseURI).length > 0 ? string(abi.encodePacked(currentBaseURI, tokenId.toString(), ".json")) : "";
    }

I want to query the URI of a token that does not exist. The test should fail and be reverted with "ERC721Metadata: URI query for nonexistent token"

Here is my test with hardhat (tokenId 1 does not exist):

  it("should not work", async function () {
    let uri = await this.deployedContract.tokenURI(1);
    console.log(uri);
    await expect(uri).to.be.revertedWith(
      "ERC721Metadata: URI query for nonexistent token"
    );
  });

I npx hardhat test then I have this error "Error: missing revert data in call exception; Transaction reverted without a reason string" followed by a huge error block.

enter image description here

To let you understand what "This.deployedContract" is:

  it("should deploy the smart contract", async function () {
    const baseURI = "ipfs://cid/";
    const Contract = await ethers.getContractFactory("test");
    this.deployedContract = await Contract.deploy(baseURI);
  });

Does someone encountered the same issue ?


Solution

  • This solution is working. I think the error came from the fact that I used "await" before "this.deployedContract.tokenURI(1)" which do not return a promise.

    enter image description here