npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bozhkovatanas/wallet-mock

v1.1.4

Published

Mock Web3 Browser wallets, like Metamask, in Playwright tests.

Downloads

1,523

Readme

Wallet Mock

Fully functional end-to-end (E2E) tests for your decentralized application (dApp). This package installs a fully operational, headless Web3 Wallet into the Playwright Browser Context. The wallet can be configured to execute on the blockchain or return mock responses. It is discoverable through EIP-6963 and leverages viem Account and Transport interfaces for easy customization.

Features

  • Create comprehensive E2E tests for your dApps, including real blockchain transactions
  • Mock specific calls or all calls to the wallet
  • All wallet actions are pre-approved by default, eliminating the need for user interaction
  • All wallet interactions are headless, meaning, no user interaction is required. You should be testing your dApp, not the wallet

Quickstart

Install

npm install -D @bozhkovatanas/wallet-mock

Setup the Mock Wallet

import { test } from "@playwright/test";
import { installMockWallet } from "@bozhkovatanas/wallet-mock";
import { privateKeyToAccount } from "viem/accounts";
import { http } from "viem";

// Replace with your actual RPC URLs
const ETHEREUM_RPC_URL = `https://YOUR_ETHEREUM_RPC_URL`;
const ARBITRUM_RPC_URL = `https://YOUR_ARBITRUM_RPC_URL`;

test.beforeEach(async ({page}) => {
    const transports = new Map<number, Transport>();
    transports.set(1, http(ETHEREUM_RPC_URL));
    transports.set(42161, http(ARBITRUM_RPC_URL));

    await installMockWallet({
        page,
        account: privateKeyToAccount(process.env.TEST_PRIVATE_KEY as Hash),
        transports,
    });
});

test("Wallet Integration Test", async ({page}) => {
    await page.getByRole("button", { name: "Log In" }).click();
    await page.getByRole("button", { name: "Choose Wallet" }).click();
    await page.getByRole("menuitem", { name: "Mock Wallet" }).click();

    // Add your test assertions here
});

Note: This setup will execute actual transactions on the blockchain without user intervention using the provided Private Key.

📚 Function: installMockWallet

The installMockWallet function is the main export of the library, designed to integrate a mock wallet into your testing setup.

Function Signature

type InstallMockWalletParamsWithBrowserContext = {
    account: LocalAccount,
    transports: Map<number, Transport>,
    browserContext: BrowserContext
};
type InstallMockWalletParamsWithPage = { 
    account: LocalAccount, 
    transports: Map<number, Transport>, 
    page: Page
};

async function installMockWallet(params: InstallMockWalletParamsWithBrowserContext | InstallMockWalletParamsWithPage): Promise<void>;

You can pass in either a BrowserContext or a Page object, depending on your Playwright testing setup.

Uniswap Demo

The Mock Wallet will show up as an EIP-6963 compatible wallet.

Testing with Hardhat

To test with a local Hardhat node:

  1. Start your local Hardhat Node:

    npx hardhat node
  2. Connect the Mock Wallet to your Hardhat Node:

    await installMockWallet({
      page,
      account: privateKeyToAccount(
        "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
      ),
      transports: new Map().set(1, http("http://127.0.0.1:8545")),
    });