PatrickAlphaC / hardhat-fund-me-fcc

82 stars 184 forks source link

TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider') when running testcase for FundMe Constructor #158

Open hariprasad9899 opened 1 year ago

hariprasad9899 commented 1 year ago

I am trying to run test cases, following the tutorial. I also remove the async function from the describe. But still getting the error, when running the test case.

Also, I have deployed the contract in the local network and testnet. There wasn't any issues. Only when running test case, it is throwing error.

What might be the reason?

const { deployments, ethers, getNamedAccounts, network } = require("hardhat");
const { assert } = require("chai");

describe("FundMe", function () {
    let fundme;
    let deployer;
    let MockV3Aggregator;
    beforeEach(async function () {
        deployer = (await getNamedAccounts()).deployer;
        await deployments.fixture(["all"]);
        fundme = await ethers.getContract("FundMe", deployer);
        MockV3Aggregator = await ethers.getContract("MockV3Aggregator", deployer);
    });

    describe("constructor", function () {
        it("this sets the aggregator mock address correctly", async function () {
            const response = await fundme.priceFeed();
            assert.equal("some rantom text", MockV3Aggregator.address);
        });
    });
});
peibo0318 commented 1 year ago

same issue here. anyone solved this?

peibo0318 commented 1 year ago

I found the issue. It seems there are some weird behaviors from ethers 6.x.x. Do the following fixed my problem:

  1. change ethers version to "^5.5.3" in package.json
  2. run "yarn install" in the terminal to reinstall the 5.5.3 version of ethers
VINURIRODRIGO commented 1 year ago

Try changing the version of ethers to ^5.7.2 in the package.json file. You can do this by running two commands: first, remove the current version of ethers using yarn remove ethers, and second, add version 5.7.2 using yarn add ethers@5.7.2

BigTava commented 1 year ago

Try changing the version of ethers to ^5.7.2 in the package.json file. You can do this by running two commands: first, remove the current version of ethers using yarn remove ethers, and second, add version 5.7.2 using yarn add ethers@5.7.2

Thanks! That solves

VanVasquez commented 1 year ago

I got this issue too... to resolve this issue, change your ethers version form version 6 to somewhere in 5.

run this: yarn

yarn remove ethers
yarn add --dev ethers@5.7.2

npm

npm uninstall ethers
npm install -D ethers@5.7.2

I think version 6 got new ways to implement this. staying in version 5 might be better for learning this

Ahmedborwin commented 1 year ago

This was actually driving me crazy. Thank you all!

GIRISHNP commented 1 year ago

@peibo0318 thanks man ! it works!

Orion516666 commented 1 year ago

ethers.js V6 has been released, so I'm providing a solution for ethers V6.

You need to update your hardhat according to the following link: https://github.com/NomicFoundation/hardhat/releases/tag/%40nomicfoundation%2Fhardhat-toolbox%403.0.0

Then modify the code to:

describe("FundMe", function () {
    let fundMe
    let deployer
    let mockV3Aggregator
    let MockV3AggregatorAddress
    beforeEach(async () => {
        deployer = (await getNamedAccounts()).deployer
        deployer = await ethers.provider.getSigner(deployer)
        const Deployments = await deployments.fixture(["all"])
        const fundMeAddress = Deployments.FundMe.address
        MockV3AggregatorAddress = Deployments.MockV3Aggregator.address
        fundMe = await ethers.getContractAt("FundMe", fundMeAddress, deployer)
        // mockV3Aggregator = await ethers.getContractAt(
        //     "MockV3Aggregator",
        //     MockV3AggregatorAddress,
        //     deployer
        // )
    })
    describe("constructor", function () {
        it("sets the aggregator addresses correctly", async () => {
            const response = await fundMe.getPriceFeed()
            console.log(response)
            // mockV3AggregatorAddress = await mockV3Aggregator.getAddress()
            assert.equal(response, MockV3AggregatorAddress)
        })
    })
})
steinitz commented 6 months ago

I ended up writing an ethers-v6-compatible getContract JavaScript function which I call in several places in this project. It returns the contract and also the signer.
Note: I've forgotten why I added the deploymentsFixtureTags section. Ignore it or remove it if you don't know why it's useful.

Below is the contents of my getContract.js file, which I placed in a utils directory.

Hopefully it's of use to someone.

const {
  deployments, 
  ethers,
} = require("hardhat")

const getContract = async (contractName, deploymentsFixtureTags) => {

  // for local testing re-use the same deployment
  if (deploymentsFixtureTags) {
    await deployments.fixture(deploymentsFixtureTags)
  }

  // console.log({contractName})
  signer = await ethers.provider.getSigner()
  // console.log({signer})

  const deployment = await deployments.get(contractName)
  // console.log({deployment})

  const contract = await ethers.getContractAt(
    contractName, 
    deployment.address, 
    signer
  ) 
  // console.log({fundMe})
  return {signer, contract};
}

module.exports = {
  getContract
}

Here's an example call site from the fund.js script file. This particular call site only needs the contract return value, and so ignores the signer return value.

...

const {getContract} = require('../utils/getContract')

...

  ({contract: fundMe} = await getContract("FundMe"))
  const transactionResponse = await fundMe.fund({
    value: valueToSend,
  })
...