我正在学习 solidity,在这里我遇到了一些测试错误。我正在使用 hardhat npx hardhat test 运行脚本测试。此错误产生错误 [ERR_REQUIRE_ESM]: require() of ES Modul...
我正在学习 solidity,在这里我遇到了一些测试错误。我正在使用 hardhat 运行脚本测试 npx hardhat test
。产生了这个错误
error Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\user\Desktop\hardtproject\node_modules\chai\chai.js from C:\Users\user\Desktop\hardtproject\test\sample-test.js not supported.
Instead change the require of chai.js in C:\Users\user\Desktop\hardtproject\test\sample-test.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\user\Desktop\hardtproject\test\sample-test.js:2:20) {
code: 'ERR_REQUIRE_ESM'
}
我正在使用节点版本 20,在我看来,我认为它接受 require 关键字。下一步该怎么做?
测试代码如下:
const { assert } = require("chai");
// the `describe` scope encapsulates an entire test called `TestModifyVariable`
// the `it` says the behavior that should be expected from the test
describe("TestModifyVar", function () {
it("should change x to 74", async function () {
// this line creates an ethers ContractFactory abstraction: https://docs.ethers.org/v5/api/contract/contract-factory/
const ModifyVar = await ethers.getContractFactory("ModifyVar");
// we then use the ContractFactory object to deploy an instance of the contract
const contract = await ModifyVar.deploy(500);
// wait for contract to be deployed and validated!
await contract.deployed();
// modify x from 10 to 1337 via this function!
await contract.setVar();
// getter for state variable x
const newX = await contract.variable();
assert.equal(newX.toNumber(), 74);
});
});
我尝试在 package.json 中添加类型模块,但没有成功。