1

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 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); }); }); 

I tried to add type module in package.json and it didn't work.

1 Answer 1

0

if you set "type": "module" to your package.json, make sure that all files have the correct .mjs extension, or .js if they are treated as ESM by the "type": "module" setting.

But the assert function (as well as expect and should from Chai) is globally available in the test environment, so you don't need to import it manually. Hardhat automatically makes these assertions available globally in your test files.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.