Skip to content

Commit

Permalink
tests: added browser-safe inspect for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jan 4, 2025
1 parent 74a68bc commit e299a31
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src.ts/_tests/test-contract-integ.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from "assert";

import { inspect } from "./utils.js";

import { ethers } from "../index.js";

Expand All @@ -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");
}
});
Expand Down
3 changes: 2 additions & 1 deletion src.ts/_tests/test-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
}
});
Expand Down
26 changes: 26 additions & 0 deletions src.ts/_tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } ]`
}

0 comments on commit e299a31

Please sign in to comment.