admin管理员组文章数量:1290226
According to the official testing documentation for Hardhat, ethers
should be available implicitly within the global scope; however, it can optionally be require
d explicitly, like so:
const { ethers } = require("hardhat");
This fails for my local project.
My package manifest seems to include the correct dependencies:
{
"dependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.1",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@openzeppelin/contracts": ".0.0-beta.0",
"chai": "^4.3.1",
"hardhat": "^2.0.11"
}
}
My unit tests file seems to match the worked example in the Hardhat documentation also:
const { ethers } = require("hardhat");
const { expect } = require("chai");
describe("Distributor.sol", function() {
it("Distribution should fail for non-owners", async function() {
const DistributorFactory = await ethers.getContractFactory("Distributor");
const Distributor = await Distributor.deploy();
Distributor.distribute([], []);
expect(await hardhatToken.totalSupply()).to.be.revertedWith("foobar");
});
});
Despite this, running the tests fails with:
$ yarn hardhat test
yarn run v1.22.5
$ /home/bob/dev/misc/token-distributor/node_modules/.bin/hardhat test
Distributor.sol
undefined
1) Distribution should fail for non-owners
0 passing (9ms)
1 failing
1) Distributor.sol
Distribution should fail for non-owners:
TypeError: Cannot read property 'getContractFactory' of undefined
at Context.<anonymous> (test/Distributor.js:8:49)
at processImmediate (internal/timers.js:461:21)
error Command failed with exit code 1.
info Visit for documentation about this mand.
How do I fix this?
According to the official testing documentation for Hardhat, ethers
should be available implicitly within the global scope; however, it can optionally be require
d explicitly, like so:
const { ethers } = require("hardhat");
This fails for my local project.
My package manifest seems to include the correct dependencies:
{
"dependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.1",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@openzeppelin/contracts": "https://github./OpenZeppelin/openzeppelin-contracts#v4.0.0-beta.0",
"chai": "^4.3.1",
"hardhat": "^2.0.11"
}
}
My unit tests file seems to match the worked example in the Hardhat documentation also:
const { ethers } = require("hardhat");
const { expect } = require("chai");
describe("Distributor.sol", function() {
it("Distribution should fail for non-owners", async function() {
const DistributorFactory = await ethers.getContractFactory("Distributor");
const Distributor = await Distributor.deploy();
Distributor.distribute([], []);
expect(await hardhatToken.totalSupply()).to.be.revertedWith("foobar");
});
});
Despite this, running the tests fails with:
$ yarn hardhat test
yarn run v1.22.5
$ /home/bob/dev/misc/token-distributor/node_modules/.bin/hardhat test
Distributor.sol
undefined
1) Distribution should fail for non-owners
0 passing (9ms)
1 failing
1) Distributor.sol
Distribution should fail for non-owners:
TypeError: Cannot read property 'getContractFactory' of undefined
at Context.<anonymous> (test/Distributor.js:8:49)
at processImmediate (internal/timers.js:461:21)
error Command failed with exit code 1.
info Visit https://yarnpkg./en/docs/cli/run for documentation about this mand.
How do I fix this?
Share Improve this question edited Apr 16, 2021 at 11:54 alcuadrado 8,5303 gold badges27 silver badges25 bronze badges asked Mar 3, 2021 at 1:45 sporejacksporejack 1012 silver badges6 bronze badges4 Answers
Reset to default 5Two things:
you need to install
ethers
separately too, as given in the instructions forhardhat-ethers
, e.g.
npm install --save-dev @nomiclabs/hardhat-ethers 'ethers@^5.0.0'
Every Hardhat plugin needs to be registered in the Hardhat config file (
hardhat.config.js
):
require("@nomiclabs/hardhat-ethers");
There is no need to remove the explicit import in your test file, however, Hardhat docs remend following this style:
const hre = require("hardhat");
const { ethers } = hre;
Add the require in your hardhat.config.js
require("@nomiclabs/hardhat-waffle");
And remove this line from your test file:
const { ethers } = require("hardhat");
Then, you can use ethers
in your tests. Hardhat looks in the config first before running tests. If you have required a package that includes ethers
you can use it in the global scope.
The above solution is now deprecated..
Use this instead: source https://hardhat/hardhat-runner/plugins/nomicfoundation-hardhat-ethers
npm install --save-dev @nomicfoundation/hardhat-ethers ethers
in your hardhat.config.js
require("@nomicfoundation/hardhat-ethers");
or hardhat.config.ts
import "@nomicfoundation/hardhat-ethers";
and you can now directly import in your test file
import { ethers } from "hardhat";
It still shows error, reading ethers undefined
, I started the project all again with yarn hardhat
and chose the first option "Create Simple Project" and it works perfectly.
本文标签: javascriptImporting ethers via Hardhat fails despite official testing documentationStack Overflow
版权声明:本文标题:javascript - Importing ethers via Hardhat fails despite official testing documentation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741454632a2379686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论