Skip to content

Commit 29d0afa

Browse files
Merge pull request #3 from johanneskares/feature/documentation
Documentation
2 parents 0b4ee57 + dd6a5a0 commit 29d0afa

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

README.md

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# Wallet Mock
2-
Fully functional E2E tests for your dApp. Installs a fully operational Mock Wallet into the [Playwright](https://github.com/microsoft/playwright) Browser Context, making it discoverable through [EIP-6963](https://eips.ethereum.org/EIPS/eip-6963) and leveraging [viem](https://github.com/wevm/viem) `Account` and `Transport` interfaces, so you can completely customize the behavior.
2+
Fully functional end-to-end (E2E) tests for your decentralized application (dApp). This package installs a fully operational Web3 Wallet into the [Playwright](https://github.com/microsoft/playwright) Browser Context. The wallet can be configured to execute on the blockchain or return mock responses. It is discoverable through [EIP-6963](https://eips.ethereum.org/EIPS/eip-6963) and leverages [viem](https://github.com/wevm/viem) `Account` and `Transport` interfaces for easy customization.
3+
4+
## Features
5+
- Create comprehensive E2E tests for your dApps, including real blockchain transactions
6+
- Mock specific calls or all calls to the wallet
7+
- All wallet actions are pre-approved by default, eliminating the need for user interaction
38

49
## Quickstart
510
```ts
11+
import { test } from "@playwright/test";
612
import { installMockWallet } from "@johanneskares/mock-wallet";
713
import { privateKeyToAccount } from "viem/accounts";
814
import { http } from "viem";
@@ -23,8 +29,52 @@ test("Your Test", async ({ page }) => {
2329
await page.getByRole("menuitem", { name: "Mock Wallet" }).click();
2430
});
2531
```
32+
> **Note:** This setup will execute actual transactions on the blockchain without user intervention using the provided Private Key.
33+
34+
### Uniswap Example
35+
The Mock Wallet will show up as an EIP-6963 compatible wallet.
36+
37+
<img width="500" alt="Screenshot Uniswap" src="https://github.com/johanneskares/wallet-mock/assets/1416628/b3d31df0-6273-42da-b00f-63bc8294a592">
38+
39+
## Mocking
40+
Here's a simple example of how to mock a specific function while using regular RPC calls for all other functions:
41+
42+
```ts
43+
await installMockWallet({
44+
page,
45+
account: privateKeyToAccount(
46+
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
47+
),
48+
transport: (config) => {
49+
return custom({
50+
request: async ({ method, params }) => {
51+
// Mock only this RPC call
52+
if (method === "eth_sendTransaction") {
53+
return "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
54+
}
55+
56+
return await http()(config).request({ method, params });
57+
},
58+
})(config);
59+
},
60+
});
61+
```
62+
63+
## Testing with Hardhat
64+
To test with a local Hardhat node:
2665

27-
Please note! This will execute actual transactions on the blockchain without user intervention using the Private Key. You can use a [Custom Transport](https://viem.sh/docs/clients/transports/custom.html) to intercept the behavior.
66+
1. Start your local Hardhat Node:
67+
```shell
68+
npx hardhat node
69+
```
2870

29-
## Rationale
30-
Why not do E2E testing with actual Browser Wallets, like Metamask? Well, you should be testing your dApp, not the implementation of a Browser Wallet. Experience shows, that E2E tests using Browser Wallets are much more flaky and unreliable.
71+
2. Connect the Mock Wallet to your Hardhat Node:
72+
```ts
73+
await installMockWallet({
74+
page,
75+
account: privateKeyToAccount(
76+
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
77+
),
78+
transport: http("http://127.0.0.1:8545"),
79+
});
80+
```

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"build": "tsc",
3939
"dev": "tsc -W",
4040
"test": "playwright test",
41-
"test-ui": "playwright test --ui"
41+
"test-ui": "playwright test --ui",
42+
"install-conventional-commit": "curl -o- https://raw.githubusercontent.com/tapsellorg/conventional-commits-git-hook/master/scripts/install.sh | sh"
4243
}
4344
}

tests/transaction.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import { expect, test } from "@playwright/test";
22
import { installMockWallet } from "./../src/installMockWallet";
33
import { privateKeyToAccount } from "viem/accounts";
4-
import { http, isHex } from "viem";
4+
import { custom, http, isHex } from "viem";
55

66
test.beforeEach(async ({ page }) => {
77
await installMockWallet({
88
page,
99
account: privateKeyToAccount(
1010
isHex(process.env.PRIVATE_KEY) ? process.env.PRIVATE_KEY : "0x",
1111
),
12-
transport: http(),
12+
transport: (config) => {
13+
return custom({
14+
request: async ({ method, params }) => {
15+
console.log("LOG", method, params);
16+
return await http()(config).request({ method, params });
17+
},
18+
})(config);
19+
},
1320
});
1421
});
1522

0 commit comments

Comments
 (0)