JavaScript Hardhat – Solving ‘Property revertedWith Does Not Exist on Type Assertion’ Error

chaihardhatjavascriptmochawaffle

I'm running a test using waffle in hardhat.

import { assert, expect } from "chai"
.
.
.

  it("can only be changed through governance", async () => {
    await expect(box.store(55)).to.be.revertedWith("Nope, you bad")
  })

However when I run tests, I run into:

test/unit/testflow.test.ts:31:39 - error TS2339: Property 'revertedWith' does not exist on type 'Assertion'.

I have the package imported in my hardhat.config.ts

import "@nomiclabs/hardhat-waffle"

And I have the package installed:

package.json

    "@nomiclabs/hardhat-waffle": "^2.0.2",

What am I missing?

Best Answer

Pre June 2022

You need to also add ethereum-waffle to your package, not just the nomiclabs one @nomiclabs/hardhat-waffle. you need both

yarn add --dev ethereum-waffle

you just only need to install it, other than that in the hardhat.config.js file this will not be "require" or "import". menas in hardhat.config.js only this will be imported

require("@nomiclabs/hardhat-waffle")

Post Just 2022

Hardhat has released a new package named hardhat-toolbox to replace hardhat-waffle.

Install it with npm install --save-dev @nomicfoundation/hardhat-toolbox or yarn add --dev @nomicfoundation/hardhat-toolbox and then import it in your hardhat.config.js like this:

require('@nomicfoundation/hardhat-toolbox')

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: '0.8.9',
  paths: {  },
  networks: {  },
}

Once done, you'll be able to use revertWith like this:

 it('my contract reverting', async () => {
    await expect(myContract.method()).to.be.revertedWith(
      'My revert error message'
    )
  })