Lesson 8: I am getting error while unit testing #5327
-
Cannot read properties of undefined (reading 'JsonRpcProvider') |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 7 replies
-
Hello replace your fundMe and mockV3Aggregator to this instead
don't use getContractAt |
Beta Was this translation helpful? Give feedback.
-
@Jay-6957 For this error npm install --save-dev ethers@^5 or yarn add --dev ethers@^5 and as @pratokko said do not use npm install -D hardhat-deploy @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers@^5 or yarn add --dev hardhat-deploy @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers@^5 and add this inside require('hardhat-deploy'); |
Beta Was this translation helpful? Give feedback.
-
Hi guys, I tried all the steps that you suggested still getting errors. please help me i am stuck here form last 1 day. my test file
my pakage.json
errror
|
Beta Was this translation helpful? Give feedback.
-
Hi guys, I tried all the steps that you suggested still getting errors. please help me i am stuck here form last 1 day. my test file
my pakage.json
my error
|
Beta Was this translation helpful? Give feedback.
-
i have correct the error for the latest version of ethers and it's working correctly
then you should have the same code in hardhat-config.js require("@nomiclabs/hardhat-waffle");
require("hardhat-gas-reporter");
require("@nomiclabs/hardhat-etherscan");
require("dotenv").config();
require("solidity-coverage");
require("hardhat-deploy");
const SEPOLIA_RPC_URL = process.env.SEPOLIA_RPC_URL || "https://eth-sepolia";
const PRIVATE_KEY = process.env.PRIVATE_KEY || "0x..";
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY || "key";
const COINMARKET_API_KEY = process.env.COINMARKET_API_KEY || "key";
module.exports = {
defaultNetwork: "hardhat",
networks: {
sepolia: {
url: SEPOLIA_RPC_URL,
accounts: [PRIVATE_KEY],
chainId: 11155111,
blockConfirmations: 6,
},
localhost: {
url: "http://127.0.0.1:8545/",
chainId: 31337,
},
},
// solidity: "0.8.8",
solidity: {
compilers: [{ version: "0.8.8" }, { version: "0.6.6" }],
},
etherscan: {
apiKey: ETHERSCAN_API_KEY,
},
gasReporter: {
enabled: false,
outputFile: "gas-report.txt",
noColors: true,
currency: "USD",
coinmarketcap: COINMARKET_API_KEY,
token: "MATIC",
},
namedAccounts: {
deployer: {
default: 0, // here this will by default take the first account as deployer
1: 0, // similarly on mainnet it will take the first account as deployer. Note though that depending on how hardhat network are configured, the account 0 on one network can be different than on another
},
},
}; then we have FundMe.test.js should be like mine and remember to replace mockV3Aggregator.address by await mockV3Aggregator.getAddress() const { deployments, getNamedAccounts, ethers } = require("hardhat");
const { assert } = require("chai");
describe("FundMe", () => {
let fundMe;
let deployer;
let mockV3Aggregator;
beforeEach(async () => {
// deploy fundMe
// using Hardhat-deploy
deployer = (await getNamedAccounts()).deployer;
await deployments.fixture(["all"]);
fundMe = await ethers.getContract("FundMe", deployer);
mockV3Aggregator = await ethers.getContract("MockV3Aggregator", deployer);
});
describe("constructor", () => {
it("sets the aggregator addresses correctly", async ()=> {
const response = await fundMe.priceFeed();
assert.equal(response, await mockV3Aggregator.getAddress());
});
});
}) Thank you and good luck <3 |
Beta Was this translation helpful? Give feedback.
@Jay-6957 For this error
Cannot read properties of undefined (reading 'JsonRpcProvider')
run this command;or
and as @pratokko said do not use
getContractAt
function instead usegetContract
function and if you havecannot find getContract function
error, then run this command;or
and add this inside
hardhat.config.js
file;