我的hardhat.config.js中有以下内容
networks: {
hardhat: {
forking: {
url: `https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
blockNumber: 14390000
}
}
}
当我运行
npx hardhat test
时,它将分叉网络而不是使用本地安全帽节点。在 CLI 中运行 npx hardhat test
时,有什么方法可以启用/禁用分叉吗?
在没有指定网络的情况下运行
npx hardhat test
会针对 hardhat
网络运行测试。在这种情况下,hardhat
网络被配置为一个分叉。
您可以使用网络助手插件(
docs)的
reset()
功能在运行时删除分叉状态。
const { loadFixture, reset } = require("@nomicfoundation/hardhat-network-helpers");
describe.only('Test group', function () {
async function deployFixture() {
console.log(`Block number non-zero before reset: ${await ethers.provider.getBlockNumber()}`);
await reset();
console.log(`Block number zero after reset: ${await ethers.provider.getBlockNumber()}`);
return;
}
it('runs the test case', async function () {
await loadFixture(deployFixture);
});
});
注意:如果您想针对不同的网络运行测试(在
hardhat.config.js
中配置),您可以指定 --network
选项:
npx hardhat test --network "myOtherNetwork"