I am following this tutorial https://hardhat.org/tutorial/testing-contracts.html and I get this unexpected error:
const { expect } = require("chai");
const { ethers } = require("ethers");
describe("Token contract", function() {
it("Deployment should assign the total supply of tokens to the owner", async function() {
const [owner] = await ethers.getSigners();
const Token = await ethers.getContractFactory("Token");
const hardhatToken = await Token.deploy();
const ownerBalance = await hardhatToken.balanceOf(owner.address);
expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);
});
});
$ npx hardhat test
TypeError: ethers.getContractFactory is not a function
Best Answer
Two things happened:
require("@nomiclabs/hardhat-waffle");
was missing inhardhat.config.js
(as mentionned at the end of https://hardhat.org/tutorial/creating-a-new-hardhat-project.html)const { ethers } = require("ethers");
should be replaced byconst { ethers } = require("hardhat");
or removed as it is available in the global scope.