尽管有官方测试文档,但通过 Hardhat 导入以太币失败
Importing ethers via Hardhat fails despite official testing documentation
根据 Hardhat 的 official testing documentation,ethers
应该在全局范围内隐式可用;但是,它可以选择显式 require
d,如下所示:
const { ethers } = require("hardhat");
我的本地项目失败了。
我的包清单似乎包含正确的依赖项:
{
"dependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.1",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@openzeppelin/contracts": "https://github.com/OpenZeppelin/openzeppelin-contracts#v4.0.0-beta.0",
"chai": "^4.3.1",
"hardhat": "^2.0.11"
}
}
我的单元测试文件似乎也与 Hardhat 文档中的工作示例相匹配:
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");
});
});
尽管如此,运行 测试失败并显示:
$ 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.com/en/docs/cli/run for documentation about this command.
我该如何解决这个问题?
在您的 hardhat.config.js
中添加要求
require("@nomiclabs/hardhat-waffle");
并从您的测试文件中删除这一行:
const { ethers } = require("hardhat");
然后,您可以在测试中使用 ethers
。 Hardhat 在 运行 测试之前首先查看配置。如果您需要包含 ethers
的软件包,您可以在全局范围内使用它。
两件事:
您也需要单独安装 ethers
,如 instructions for hardhat-ethers
中所述,例如
npm install --save-dev @nomiclabs/hardhat-ethers 'ethers@^5.0.0'
每个 Hardhat 插件都需要在 Hardhat 配置文件中注册 (hardhat.config.js
):
require("@nomiclabs/hardhat-ethers");
无需删除测试文件中的显式导入,但是,Hardhat 文档建议遵循以下样式:
const hre = require("hardhat);
const { ethers } = hre;
根据 Hardhat 的 official testing documentation,ethers
应该在全局范围内隐式可用;但是,它可以选择显式 require
d,如下所示:
const { ethers } = require("hardhat");
我的本地项目失败了。
我的包清单似乎包含正确的依赖项:
{
"dependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.1",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@openzeppelin/contracts": "https://github.com/OpenZeppelin/openzeppelin-contracts#v4.0.0-beta.0",
"chai": "^4.3.1",
"hardhat": "^2.0.11"
}
}
我的单元测试文件似乎也与 Hardhat 文档中的工作示例相匹配:
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");
});
});
尽管如此,运行 测试失败并显示:
$ 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.com/en/docs/cli/run for documentation about this command.
我该如何解决这个问题?
在您的 hardhat.config.js
require("@nomiclabs/hardhat-waffle");
并从您的测试文件中删除这一行:
const { ethers } = require("hardhat");
然后,您可以在测试中使用 ethers
。 Hardhat 在 运行 测试之前首先查看配置。如果您需要包含 ethers
的软件包,您可以在全局范围内使用它。
两件事:
您也需要单独安装
ethers
,如 instructions forhardhat-ethers
中所述,例如
npm install --save-dev @nomiclabs/hardhat-ethers 'ethers@^5.0.0'
每个 Hardhat 插件都需要在 Hardhat 配置文件中注册 (
hardhat.config.js
):
require("@nomiclabs/hardhat-ethers");
无需删除测试文件中的显式导入,但是,Hardhat 文档建议遵循以下样式:
const hre = require("hardhat);
const { ethers } = hre;