hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

Hardhat issue: `rejectedWith` assertion does not fail when no message is provided

Open thecil opened this issue 1 year ago • 2 comments

Version of Hardhat

2.22.8

What happened?

I think there might be an error with rejectedWith assertion, if you leave it empty or empty string (rejectedWith("")), the unit test will take it has succeed, where instead it should fail because of the miss match from the expected error and actual error.

Using the template example: (keep in mind that main page unit test is still using revertedWith on the docs, but the hardhat project downloaded when initializing a project to get the template contract and unit test are different).

https://hardhat.org/hardhat-runner/docs/getting-started#overview

The withdraw function from the Lock contract contain this condition

require(block.timestamp >= unlockTime, "You can't withdraw yet");

Now at the unit test in code line 78, we have:

      it("Should revert with the right error if called too soon", async function () {
        const { lock } = await loadFixture(deployOneYearLockFixture);

        await expect(lock.write.withdraw()).to.be.rejectedWith(
          "You can't withdraw yet"
        );
      });

If we leave the expected rejected message empty and execute the test, it will succeed.

    // this should throw because the expected empty error msg is different than actual error message
     await expect(lock.write.withdraw()).to.be.rejectedWith();

heres my package.json in case its needed.

{
  "name": "hardhat-project",
  "devDependencies": {
    "@nomicfoundation/hardhat-toolbox-viem": "^3.0.0",
    "hardhat": "^2.22.8"
  },
  "dependencies": {
    "@openzeppelin/contracts": "^5.0.2"
  }
}

Minimal reproduction steps

  • run hardhat init and choose "Create a Typescript project (whit Viem)".
  • go to the Lock contract unit test.
  • find any assertion that use rejectedWith that contains a custom message. (like test case at codeline 78).
  • delete the custom error message, or leave it with an empty string.
  • run the unit test, it should fail, although it pass.

Search terms

bug

thecil avatar Aug 12 '24 19:08 thecil

Hey @thecil, thanks for the reproduction steps. When I do a Hardhat init, create a javascript project, then remove the assert message at line 78 in lock.js I get an error on running the tests.

Can I confirm what you mean by "download hardhat project template"?

kanej avatar Aug 13 '24 08:08 kanej

Hey @thecil, thanks for the reproduction steps. When I do a Hardhat init, create a javascript project, then remove the assert message at line 78 in lock.js I get an error on running the tests.

Can I confirm what you mean by "download hardhat project template"?

Yes, sorry for the miss understanding, when we do hardhat init, I choose the option "Create a Typescript project (whit Viem)".

maybe the error is not on the javascript project and only on typescript? not sure tbh

thecil avatar Aug 13 '24 15:08 thecil

Hey I was able to reproduce locally. If you remove the message or put in an empty message the rejectedWith passes. I found this surprising as well.

hardhat-chai-matchers which is built into the Hardhat toolbox leverages the chai assertion library and its helper library chai-as-promised.

The semantics of rejectedWith is defined in chai-as-promised, if you pass in nothing for the message it treats it like a rejected: https://github.com/chaijs/chai-as-promised/blob/85ce33410b3e31c7508e44ede3e5cf4df4244df8/lib/chai-as-promised.js#L158

kanej avatar Sep 19 '24 10:09 kanej

We don't intend to make any changes to our usage of chai, so I am going to close this issue as I think this is expected behavior.

kanej avatar Sep 19 '24 10:09 kanej