Search code examples
javascriptethereumhardhatether

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


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?

My Test File


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);
        });
    });
});

00-deploy-mock:

const { network } = require("hardhat");
const { developmentChains, INITIAL_ANSWER, DECIMALS } = require("../helper-hardhat-config");

module.exports = async ({ getNamedAccounts, deployments }) => {
    const { deploy, log } = deployments;
    const { deployer } = await getNamedAccounts();
    const chainId = network.config.chainId;

    if (developmentChains.includes(network.name)) {
        log(`Local Network detected! Deploying mocks..`);
        await deploy("MockV3Aggregator", {
            // contract: "MockV3Aggregator",
            from: deployer,
            log: true,
            args: [DECIMALS, INITIAL_ANSWER],
        });
        log("Mocks Deployed!! ");
        log("_____________________________________");
    }
};

module.exports.tags = ["all", "mocks"];


01-deploy-funndme


const { network } = require("hardhat");
const { networkConfig, developmentChains } = require("../helper-hardhat-config");
const { verify } = require("../utils/verify");

module.exports = async ({ getNamedAccounts, deployments }) => {
    const { deploy, log } = deployments;
    const { deployer } = await getNamedAccounts();
    const chainId = network.config.chainId;

    let ethUsdPriceFeed;
    // const ethUsdPriceFeed = networkConfig[chainId]["ethUsdPriceFeed"];

    if (developmentChains.includes(network.name)) {
        const mockAggregator = await deployments.get("MockV3Aggregator");
        ethUsdPriceFeed = mockAggregator.address;
    } else {
        ethUsdPriceFeed = networkConfig[chainId]["ethUsdPriceFeed"];
    }

    const args = [ethUsdPriceFeed];
    const fundMe = await deploy("FundMe", {
        from: deployer,
        args: args, // price feed address
        logs: true,
        waitConfirmations: network.config.blockConfirmations || 1,
    });

    console.log(`Deploying fund me contract ...........`);

    if (!developmentChains.includes(network.name) && process.env.ETHERSCAN_API) {
        await verify(fundMe.address, args);
    }
    log("______________________________________");
};

module.exports.tags = ["all", "fundme"];


hardhat-config


require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-solhint");
require("hardhat-deploy");
// require("dotenv").config();
// require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-etherscan");
require("hardhat-gas-reporter");
require("solidity-coverage");
require("dotenv").config();

/** @type import('hardhat/config').HardhatUserConfig */

const TEST_RPC_URL = process.env.TEST_RPC_URL;
const TEST_PRIVATE_KEY = process.env.TEST_PRIVATE_KEY;
const ETHERSCAN_API = process.env.ETHERSCAN_API;
const COINMARKET_API = process.env.COINMARKET_API;

module.exports = {
    solidity: "0.8.8",
    solidity: {
        compilers: [{ version: "0.8.8" }, { version: "0.6.6" }],
    },
    namedAccounts: {
        deployer: {
            default: 0,
        },
    },
    defaultNetwork: "hardhat",
    networks: {
        goerli: {
            url: TEST_RPC_URL,
            accounts: [TEST_PRIVATE_KEY],
            chainId: 5,
            blockConfirmations: 3,
        },
        localhost: {
            url: "http://127.0.0.1:8545/",
            chainId: 31337,
        },
    },
    etherscan: {
        apiKey: ETHERSCAN_API,
    },
    gasReporter: {
        enabled: true,
        noColors: true,
        outputFile: "gasReport.txt",
        currency: "INR",
        coinmarketcap: COINMARKET_API,
    },
    mocha: {
        timeout: 500000,
    },
};


helper hardhat config


const networkConfig = {
    5: {
        name: "goerli",
        ethUsdPriceFeed: "0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e",
    },
};

const developmentChains = ["local", "hardhat"];
const DECIMALS = 8;
const INITIAL_ANSWER = 200000000;

module.exports = {
    networkConfig,
    developmentChains,
    DECIMALS,
    INITIAL_ANSWER,
};



Solution

  • I had the same problem, and I demoted ethers to version 5.5.3 to run successfully. I think hardhat is not compatible with the latest version of ethers. yarn add -D [email protected]