diff --git a/v-next/hardhat-test-utils/src/console.ts b/v-next/hardhat-test-utils/src/console.ts new file mode 100644 index 0000000000..75e06d96b1 --- /dev/null +++ b/v-next/hardhat-test-utils/src/console.ts @@ -0,0 +1,36 @@ +import { after, before } from "node:test"; + +/** + * Disables the console functions that directly interact with stdout/stderr. + * + * This function is useful when you want to test a function that use console + * logging but you don't want to see the output in the test report. + * In particular, we have observed that using console.log can cause errors like: + * - Error: Unable to deserialize cloned data due to invalid or unsupported version. + * + * This function overwrites the original console.log/console.warn/console.dir + * with no-op functions. + * + * If we ever want to inspect the stdout/stderr, we could accept + * NodeJS.WritableStream arguments and write formatted messages to them instead. + * + * Another interesting extension to this function would be not to disable the + * console streams if a DEBUG flag is set in the environment. + */ +export function disableConsole(): void { + const originalLog = console.log; + const originalWarn = console.warn; + const originalDir = console.dir; + + before(() => { + console.log = () => {}; + console.warn = () => {}; + console.dir = () => {}; + }); + + after(() => { + console.log = originalLog; + console.warn = originalWarn; + console.dir = originalDir; + }); +} diff --git a/v-next/hardhat-test-utils/src/index.ts b/v-next/hardhat-test-utils/src/index.ts index 3c40c05c6e..ca3f031417 100644 --- a/v-next/hardhat-test-utils/src/index.ts +++ b/v-next/hardhat-test-utils/src/index.ts @@ -1,3 +1,4 @@ +export * from "./console.js"; export * from "./fixture-projects.js"; -export * from "./hardhat-error.js"; export * from "./fs.js"; +export * from "./hardhat-error.js"; diff --git a/v-next/hardhat/test/internal/cli/init/init.ts b/v-next/hardhat/test/internal/cli/init/init.ts index c9d20ef548..18fed9d5ce 100644 --- a/v-next/hardhat/test/internal/cli/init/init.ts +++ b/v-next/hardhat/test/internal/cli/init/init.ts @@ -11,6 +11,7 @@ import { } from "@ignored/hardhat-vnext-utils/fs"; import { assertRejectsWithHardhatError, + disableConsole, useTmpDir, } from "@nomicfoundation/hardhat-test-utils"; @@ -27,6 +28,8 @@ import { getTemplates } from "../../../../src/internal/cli/init/template.js"; // NOTE: This uses network to access the npm registry describe("printWelcomeMessage", () => { + disableConsole(); + it("should not throw if latest version of hardhat cannot be retrieved from the registry", async () => { await printWelcomeMessage(); }); @@ -109,6 +112,8 @@ describe("ensureProjectPackageJson", () => { describe("copyProjectFiles", () => { useTmpDir("copyProjectFiles"); + disableConsole(); + describe("when force is true", () => { it("should copy the template files to the workspace and overwrite existing files", async () => { const template = await getTemplate("empty-typescript"); @@ -150,6 +155,8 @@ describe("copyProjectFiles", () => { describe("installProjectDependencies", () => { useTmpDir("installProjectDependencies"); + disableConsole(); + describe("when install is true", () => { // This test is skipped because installing dependencies over the network is slow it.skip("should install the project dependencies", async () => { @@ -176,6 +183,8 @@ describe("installProjectDependencies", () => { describe("initHardhat", async () => { useTmpDir("initHardhat"); + disableConsole(); + const templates = await getTemplates(); for (const template of templates) {