最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: Invalid Chai property: revertedWith - Stack Overflow

programmeradmin3浏览0评论

I want to test my contract FundMe.sol but how it takes off FundMe.test.js an error pops up this

My console

FundMe
    constructor
Development network detected! Deploying mocks...
      ✓ Should set the aggregator addresses correctly
    fund

      1) Should fail if you don't send enough ETH


  1 passing (977ms)
  1 failing

  1) FundMe
       fund
         Should fail if you don't send enough ETH:
     Error: Invalid Chai property: revertedWith
      at Object.proxyGetter [as get] (node_modules/chai/lib/chai/utils/proxify.js:78:17)
      at Context.<anonymous> (test/unit/FundMe.test.js:29:46)

the file where the test is written to the file FundMe.sol

FundMe.test.js

const { deployments, ethers, getNamedAccounts } = require("hardhat")
const { assert, expect, revertedWith } = require("chai")

describe("FundMe", async function() {
    let fundMe
    let deployer
    let mockV3Aggregator
    beforeEach(async function() {
        deployer = (await getNamedAccounts()).deployer
        await deployments.fixture(["all"]) // deploy all contracts using deployment.fixture()
        fundMe = await ethers.getContract("FundMe", deployer)
        mockV3Aggregator = await ethers.getContract(
            "MockV3Aggregator",
            deployer
        )
    })

    describe("constructor", async function() {
        it("Should set the aggregator addresses correctly", async function() {
            const response = await fundMe.priceFeed()
            assert.equal(response, mockV3Aggregator.address)
        })
    })

    describe("fund", async function() {
        it("Should fail if you don't send enough ETH", async function() {
            await expect(fundMe.fund()).to.be.revertedWith("You need to spend more ETH!")
        })
    })
})

What am I doing wrong?

I want to test my contract FundMe.sol but how it takes off FundMe.test.js an error pops up this

My console

FundMe
    constructor
Development network detected! Deploying mocks...
      ✓ Should set the aggregator addresses correctly
    fund

      1) Should fail if you don't send enough ETH


  1 passing (977ms)
  1 failing

  1) FundMe
       fund
         Should fail if you don't send enough ETH:
     Error: Invalid Chai property: revertedWith
      at Object.proxyGetter [as get] (node_modules/chai/lib/chai/utils/proxify.js:78:17)
      at Context.<anonymous> (test/unit/FundMe.test.js:29:46)

the file where the test is written to the file FundMe.sol

FundMe.test.js

const { deployments, ethers, getNamedAccounts } = require("hardhat")
const { assert, expect, revertedWith } = require("chai")

describe("FundMe", async function() {
    let fundMe
    let deployer
    let mockV3Aggregator
    beforeEach(async function() {
        deployer = (await getNamedAccounts()).deployer
        await deployments.fixture(["all"]) // deploy all contracts using deployment.fixture()
        fundMe = await ethers.getContract("FundMe", deployer)
        mockV3Aggregator = await ethers.getContract(
            "MockV3Aggregator",
            deployer
        )
    })

    describe("constructor", async function() {
        it("Should set the aggregator addresses correctly", async function() {
            const response = await fundMe.priceFeed()
            assert.equal(response, mockV3Aggregator.address)
        })
    })

    describe("fund", async function() {
        it("Should fail if you don't send enough ETH", async function() {
            await expect(fundMe.fund()).to.be.revertedWith("You need to spend more ETH!")
        })
    })
})

What am I doing wrong?

Share Improve this question asked Aug 8, 2022 at 9:18 Paweł StachPaweł Stach 1131 gold badge2 silver badges5 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 6

You just need to install hardhat-chai-matchers like this:

$ yarn add --dev @nomicfoundation/hardhat-chai-matchers

Or

npm install --save-dev @nomicfoundation/hardhat-chai-matchers

And then add this line to your hardhat.config.js:

require("@nomicfoundation/hardhat-chai-matchers");

More info: https://hardhat/hardhat-chai-matchers/docs/overview

You need to add ethereum-waffle to your packages. With @nomiclabs/hardhat-waffle being installed it is not working!

yarn add --dev ethereum-waffle

Once done, you'll be able to use revertedWith.

Recent hardhat's new package hardhat-toolbox has replaced few basic packages here

The revertedWith() is not actually a part of Chai, it es from hardhat, make sure you have also set const {ethers} = require("hardhat") at the top of your script

for example:

const { ethers } = require("hardhat");
const { expect } = require("chai");

You don't have to import revertedWith, that is what is causing your Error: Invalid Chai property: revertedWith error.

Try changing your chai import line to this:

const { assert, expect } = require("chai")

Refer to: https://ethereum.stackexchange./questions/120984/property-revertedwith-does-not-exist-on-type-assertion

Post June 2022

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')

You can also try:

const chai = require("chai")             
const { solidity } = require("ethereum-waffle")
chai.use(solidity)

This is a very dynamic situation as the interdependency changes so fast.

As of 2023/03/22, I ran

[email protected]
[email protected]
@nomicfoundation/[email protected]

To make expect(..).to.be.RevertedWith(..) work, I only needed

require("@nomicfoundation/hardhat-chai-matchers")

I DID NOT need

yarn add ethereum-waffle
yarn add @nomiclabs/hardhat-waffle
发布评论

评论列表(0)

  1. 暂无评论