Skip to content

Commit

Permalink
Merge pull request #5807 from NomicFoundation/fix/deflake-init-tests
Browse files Browse the repository at this point in the history
fix: disable console for init tests
  • Loading branch information
galargh authored Oct 7, 2024
2 parents 102df5c + 48c1a83 commit 86aa02f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
36 changes: 36 additions & 0 deletions v-next/hardhat-test-utils/src/console.ts
Original file line number Diff line number Diff line change
@@ -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;
});
}
3 changes: 2 additions & 1 deletion v-next/hardhat-test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
9 changes: 9 additions & 0 deletions v-next/hardhat/test/internal/cli/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "@ignored/hardhat-vnext-utils/fs";
import {
assertRejectsWithHardhatError,
disableConsole,
useTmpDir,
} from "@nomicfoundation/hardhat-test-utils";

Expand All @@ -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();
});
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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 () => {
Expand All @@ -176,6 +183,8 @@ describe("installProjectDependencies", () => {
describe("initHardhat", async () => {
useTmpDir("initHardhat");

disableConsole();

const templates = await getTemplates();

for (const template of templates) {
Expand Down

0 comments on commit 86aa02f

Please sign in to comment.