From e299a31ecb9017baf07c58bb927c2e718ebeb447 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Sat, 4 Jan 2025 15:54:00 -0500 Subject: [PATCH] tests: added browser-safe inspect for tests --- src.ts/_tests/test-contract-integ.ts | 3 ++- src.ts/_tests/test-contract.ts | 3 ++- src.ts/_tests/utils.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src.ts/_tests/test-contract-integ.ts b/src.ts/_tests/test-contract-integ.ts index 5b8ed3e09..52a9c6944 100644 --- a/src.ts/_tests/test-contract-integ.ts +++ b/src.ts/_tests/test-contract-integ.ts @@ -1,5 +1,6 @@ import assert from "assert"; +import { inspect } from "./utils.js"; import { ethers } from "../index.js"; @@ -13,7 +14,7 @@ describe("Tests contract integration", function() { const provider = new ethers.JsonRpcProvider("http:/\/127.0.0.1:8545"); provider.on("error", (error: any) => { if (error && error.event === "initial-network-discovery") { - console.dir(error.info, { depth: null }); + console.log(inspect(error)); provider.off("error"); } }); diff --git a/src.ts/_tests/test-contract.ts b/src.ts/_tests/test-contract.ts index cc2563f62..5d51082c2 100644 --- a/src.ts/_tests/test-contract.ts +++ b/src.ts/_tests/test-contract.ts @@ -2,6 +2,7 @@ import assert from "assert"; import { getProvider, setupProviders } from "./create-provider.js"; +import { inspect } from "./utils.js"; import { Contract, ContractFactory, EventLog, isError, JsonRpcProvider, @@ -267,7 +268,7 @@ describe("Test Typed Contract Interaction", function() { const provider = new JsonRpcProvider("http:/\/127.0.0.1:8545"); provider.on("error", (error: any) => { if (error && error.event === "initial-network-discovery") { - console.dir(error, { depth: null }); + console.log(inspect(error)); provider.off("error"); } }); diff --git a/src.ts/_tests/utils.ts b/src.ts/_tests/utils.ts index 9fce9b7db..dc4d7cd54 100644 --- a/src.ts/_tests/utils.ts +++ b/src.ts/_tests/utils.ts @@ -133,3 +133,29 @@ export class Stats { export const stats = new Stats(_guard); */ + + +export function inspect(value: any): string { + if (Array.isArray(value)) { + return "[" + value.map((v) => inspect(v)).join(", ") + "]"; + } + + switch (typeof(value)) { + case "bigint": + return value.toString() + "n"; + case "boolean": + case "number": + case "string": + return JSON.stringify(value); + case "symbol": + return `[Symbol ${ String(value) }]` + case "object": + if (value == null) { return "null"; } + return "{ " + Object.keys(value).map((key) => { + return `${ key }=${ inspect(value[key]) }`; + }).join(", ") + " }"; + } + + return `[ unknown type: ${ value } ]` +} +