4

I am expecting a revert from an Imported Ownable.sol contract since the executing address is not the owner of the function but the test fails without matching the error message thrown from the Ownable.sol contract.

HardHad Config:

const path = require('path');
require('@nomiclabs/hardhat-ethers');
require("@nomiclabs/hardhat-truffle5");
require('@nomicfoundation/hardhat-toolbox')
require('dotenv').config({path: path.join(__dirname, '../.env')});
module.exports = {
  networks: {
    hardhat: {
      forking: {
        url : (process.env.INFURA_KOVAN_URL + process.env.INFURA_API_KEY),
      }
    }
  },
  solidity: "0.8.9",
};

Smart Contract:

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

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Custodian is Ownable {

    function helloWorld() public pure returns(string memory){
        return "Hello World";
    }

    function goodByeWorld() public view onlyOwner returns(string memory){
        return "Goodbye World";
    }
}

JS Test:

it('goodbye world from owner', async ()=> {
    await expect(this.custodian.goodByeWorld({ from: other })).to.be.revertedWith("Ownable: caller is not the owner")
  });

Error:

Contract: Custodian goodbye world from owner: Error: VM Exception while processing transaction: reverted with reason string 'Ownable: caller is not the owner'

3 Answers 3

2

I ran into a very similar error as this, and after a lot of searching, this discussion proved helpful to me, specifically this comment here

The important bit is that apparently BigNumbers matchers use expect(await), all other matchers are shown using await expect().

Also worth considering this comment about if the reversion is require or not.

1

I still haven't figured out why. But moving the await in and out of the expect function works sometimes differently.

Other answers included moving the await inside expect, which works sometimes. In one of my cases, removing it completely worked too.

It's worth trying this out too:

it('goodbye world from owner', async ()=> {
    expect(this.custodian.goodByeWorld({ from: other })).to.be.revertedWith("Ownable: caller is not the owner")
});

NOTE: I don't have an explanation for it, tho. If you do, please let us know.

0

Move the await to inside the expect, like so.

it('goodbye world from owner', async ()=> {
    expect(await this.custodian.goodByeWorld({ from: other })).to.be.revertedWith("Ownable: caller is not the owner")
  });
2
  • 1
    I tried but its giving me the same issue. Sep 6, 2022 at 13:48
  • 1
    Although @Kenzo Agada is not completely wrong, this worked for me too once. I still haven't figured out why. But moving the await in and out works sometimes differently. In one of my cases removing it completely worked for me too. I don't have an explanation for it tho.
    – BonisTech
    Jan 29 at 11:03

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.