Skip to content

Commit

Permalink
feat: add verbose mode (#15)
Browse files Browse the repository at this point in the history
* feat: add verbose mode

* feat: remove useless bacjline
  • Loading branch information
VGLoic authored Feb 19, 2022
1 parent d53ca20 commit 18ce4bf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
34 changes: 34 additions & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ import { MockRequest } from "./types";
type Subscriber = (args: unknown) => unknown;
type Topics = Record<string, Subscriber[]>;

type ProviderConstructorArgs = {
verbose?: boolean;
}

export class Provider {
public requestMocks: Record<string, MockRequest[]> = {};

public topics: Topics = {};

public verbose: boolean;

constructor({ verbose }: ProviderConstructorArgs) {
this.verbose = Boolean(verbose);
}

public on(eventName: string, callback: Subscriber) {
if (this.topics[eventName]) {
this.topics[eventName].push(callback);
Expand Down Expand Up @@ -44,6 +54,9 @@ export class Provider {
}) {
const promise = new Promise<{ data: unknown; callback?: (data?: unknown, params?: unknown[]) => void }>((resolve, reject) => {
const mock = this.findMock(method, params);
if (this.verbose) {
this.logRequest(method, params, mock);
}
if (!mock) {
resolve({ data: undefined });
return;
Expand Down Expand Up @@ -79,4 +92,25 @@ export class Provider {
const standardMock = mocks.find((mock) => !Boolean(mock.condition));
return standardMock || null;
}

private logRequest(method: string, params: unknown[], mock: MockRequest | null) {
let mockDescription: string;
if (!mock) {
mockDescription = "No mock has been found. 'undefined' will be resolved.";
} else {
mockDescription = `
Mock found:
- Data: ${JSON.stringify(mock.data, null, 4)}
- Persistent: ${mock.persistent ? "yes" : "no"}
- Conditional: ${mock.condition ? "yes" : "no"}
- Should throw: ${mock.shouldThrow ? "yes" : "no"}
`
}
console.log(`
#### [eth-testing] - start log ####
Request intercepted: { method: ${method}, params: ${params} }
${mockDescription}
#### [eth-testing] - end log ####
`)
}
}
14 changes: 8 additions & 6 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ type MockRequestAccountsOptions = {

type SetupOptions = {
providerType: "MetaMask" | "WalletConnect" | "default";
verbose?: boolean;
};

const defaultSetupOptions: SetupOptions = {
providerType: "default",
verbose: false
};

export function setupEthTesting(options: SetupOptions = defaultSetupOptions) {
export function setupEthTesting({ providerType, verbose }: SetupOptions = defaultSetupOptions) {
const provider =
options.providerType === "MetaMask"
? new MetaMaskProvider()
: options.providerType === "WalletConnect"
? new WalletConnectProvider()
: new Provider();
providerType === "MetaMask"
? new MetaMaskProvider({ verbose })
: providerType === "WalletConnect"
? new WalletConnectProvider({ verbose })
: new Provider({ verbose });

const mockManager = new MockManager(provider);

Expand Down

0 comments on commit 18ce4bf

Please sign in to comment.