I am learning solidity and here I encountered some test error.I was running script test using hardhat npx hardhat test
.This error was produced
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'}
I am using node version 20 and in my wiew I was thinking it accepts require keyword. what to do next?
Test code was this:
const { assert } = require("chai");// the `describe` scope encapsulates an entire test called `TestModifyVariable`// the `it` says the behavior that should be expected from the testdescribe("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); });});
I tried to add type module in package.json and it didn't work.