TypeError: ethers.getContract is not a function
| I tried all the solutions in the internet but no luck :(
#5832
-
Lesson 7: Timestamp 11:18:10 My Unit Test file const { assert } = require("chai");
const { deployments, ethers, getNamedAccounts } = require("hardhat");
describe("FundMe", function () {
let fundMe;
let deployer;
let mockV3Aggregator;
beforeEach(async function () {
//deploy contract using hardhat
deployer = (await getNamedAccounts()).deployer;
await deployments.fixture("all");
fundMe = await ethers.getContract("FundMe", deployer);
mockV3Aggregator = await ethers.getContract("MockV3Aggregator", deployer);
})
describe("constructor", function () {
it("sets the aggregator address correctly", async function () {
const response = await fundMe.priceFeed();
assert.equal(response, mockV3Aggregator.address);
})
})
}) Command I ran into terminal: yarn hardhat test Error I got each time: yarn run v1.22.19
$ /home/pallab/Documents/development/web3/freecodecamp/hardhat-fcc/hardhat-fundme/node_modules/.bin/hardhat test
FundMe
constructor
1) "before each" hook for "sets the aggregator address correctly"
0 passing (2s)
1 failing
1) FundMe
"before each" hook for "sets the aggregator address correctly":
TypeError: ethers.getContract is not a function
at Context.<anonymous> (test/unit/FundMe.test.js:13:27)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at runNextTicks (node:internal/process/task_queues:64:3)
at listOnTimeout (node:internal/timers:538:9)
at processTimers (node:internal/timers:512:7)
error Command failed with exit code 1. My hardhat config file require("@nomicfoundation/hardhat-toolbox");
require("@nomicfoundation/hardhat-verify");
require('hardhat-gas-reporter');
require("hardhat-deploy");
require('dotenv').config();
const SEPOLIA_RPC_URL = process.env.SEPOLIA_RPC_URL;
const SEPOLIA_PRIVATE_KEY = process.env.SEPOLIA_PRIVATE_KEY;
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY;
const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY;
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
// solidity: "0.8.18",
solidity: {
compilers: [
{ version: "0.8.8" },
{ version: "0.6.6" }
]
},
defaultNetwork: "hardhat",
networks: {
localhost: {
url: "http://127.0.0.1:8545",
chainId: 31337
},
sepolia: {
url: SEPOLIA_RPC_URL,
accounts: [SEPOLIA_PRIVATE_KEY],
chainId: 11155111,
blockConfirmations: 5
}
},
namedAccounts: {
deployer: {
default: 0
}
},
etherscan: {
apiKey: ETHERSCAN_API_KEY
},
gasReporter: {
enabled: true,
outputFile: "./gas-report.txt",
noColors: true,
currency: "USD",
coinmarketcap: COINMARKETCAP_API_KEY
}
}; I am also attaching my github repo of the same project: https://github.com/pallab-nandi/hh-fundme-fcc |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Well After a long long research on the error I finally came to an answer. And this will works with for everyone of those who are using new version of hardhat and ethersJs. require("@nomicfoundation/hardhat-toolbox");
require("@nomicfoundation/hardhat-verify");
require('hardhat-gas-reporter');
require("hardhat-deploy");
require('@nomicfoundation/hardhat-ethers');
require('hardhat-deploy-ethers');
/*
your other configurations
*/ |
Beta Was this translation helpful? Give feedback.
-
I would also like to mention that |
Beta Was this translation helpful? Give feedback.
-
Use get const {deployments, ethers, getNamedAccounts} = require("hardhat");
const {assert} = require("chai");
describe ("FundMe",async function () {
let fundMe, deployer, mockV3AggregatorAddress;
beforeEach(async function () {
deployer = (await getNamedAccounts()).deployer;
await deployments.fixture(["all"]); //deploy contracts
//Get the deployment corresponding to FundMe and then get
//the address at which the contract is deployed
const fundMeAtAddress = (await deployments.get("FundMe")).address;
//Get the contract
fundMe = await ethers.getContractAt("FundMe",fundMeAtAddress);
});
describe("constructor", async function () {
//test for constructor
it ( "sets the aggregator address correctly",async function() {
//Call a function from contract to get the pricefeed
//Note: priceFeed is a private storage in the contract and
//a get function is created to return it to the caller
const response = await fundMe.getPriceFeed();
mockV3AggregatorAddress = (await deployments.get("MockV3Aggregator")).address;
assert.equal(response,mockV3AggregatorAddress);
});
});
}); The detailed documentation on |
Beta Was this translation helpful? Give feedback.
-
One more solution that worked for me was |
Beta Was this translation helpful? Give feedback.
Well After a long long research on the error I finally came to an answer. And this will works with for everyone of those who are using new version of hardhat and ethersJs.
basically on
hardhat.config.js
file do this thing require at the above. So every code will work just fine: