Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disable console for init tests #5807

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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