0

When I run tests in Hardhat I get errors as noted in this question.

In the Hardhat docs they suggest it is possible to use @babel/register, which is supposed to compile javascript on the fly.

When I add @babel/register and include require("@babel/register"); in a test file, that test file no longer complains, but now something internal to Hardhat complains.

$ npx hardhat test
An unexpected error occurred:

.../solidity/src/utils/helpers.js:55
export async function aSymbol() {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Module._compile (/Users/dan/work/lore/lore/solidity/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.newLoader [as .js] (/Users/dan/work/lore/lore/solidity/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)

How do I use Babel with Hardhat to properly compile ESModule import for use in the Hardhat tests that use CommonJS require?

dfrankow
  • 18,326
  • 38
  • 134
  • 193

0 Answers0