Skip to content

Commit

Permalink
try automated build
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskares committed Jun 13, 2024
1 parent 56bae51 commit dfe347f
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 133 deletions.
44 changes: 26 additions & 18 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
name: Playwright Tests
on:
push:
branches: [ main, master ]
branches:
- main
pull_request:
branches: [ main, master ]
branches:
- main
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
- name: Bump version and publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
npm run build
npm run publish
32 changes: 22 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 24 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
{
"name": "wallet-mock",
"version": "0.1.0",
"description": "Mock Web3 Browser wallets, like Metamask, in Playwright tests",
"name": "@johanneskares/wallet-mock",
"version": "0.1.1",
"description": "Mock Web3 Browser wallets, like Metamask, in Playwright tests.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"keywords": [],
"author": "",
"keywords": [
"web3",
"wallet",
"mock",
"playwright",
"metamask",
"ethereum",
"dapp",
"testing"
],
"author": "Johannes Kares",
"license": "ISC",
"devDependencies": {
"@playwright/test": "^1.44.1",
"@types/node": "^20.14.2",
"prettier": "^3.3.2",
"typescript": "^5.4.5"
},
"namespace": "@johanneskares",
"dependencies": {
"typescript": "^5.4.5",
"viem": "^2.13.8"
},
"repository": {
"type": "git",
"url": "git+https://github.com/johanneskares/wallet-mock.git"
},
"namespace": "@johanneskares",
"dependencies": {},
"peerDependencies": {
"@playwright/test": "^1.44.1"
"@playwright/test": "^1.44.1",
"viem": "^2.13.8"
},
"scripts": {
"build": "tsc",
"publish": "npm publish --access public",
"dev": "tsc -W",
"test": "playwright test",
"test-ui": "playwright test --ui"
Expand Down
79 changes: 79 additions & 0 deletions src/createWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
LocalAccount,
Hex,
Transport,
createWalletClient,
fromHex,
} from "viem";
import * as chains from "viem/chains";

export type Wallet = ReturnType<typeof createWallet>;

export function createWallet(account: LocalAccount, transport: Transport) {
let chainId: string | undefined;

return {
request: async ({
method,
params,
}: {
method: string;
params?: Array<unknown>;
}) => {
try {
const client = createWalletClient({
account,
chain: getChain(chainId),
transport,
});

if (method === "eth_accounts" || method === "eth_requestAccounts") {
return client.getAddresses();
}

if (
method === "wallet_requestPermissions" ||
method === "wallet_revokePermissions"
) {
return [{ parentCapability: "eth_accounts" }];
}

if (method === "wallet_switchEthereumChain") {
chainId = (params?.[0] as any).chainId;
return null;
}

if (method === "personal_sign") {
return client.account.signMessage({
message: {
raw: params?.[0] as Hex,
},
});
}

return await client.request({
method: method as any,
params: params as any,
});
} catch (error) {
console.error("error", error);
return null;
}
},
};
}

function getChain(chainIdHex: string | undefined) {
if (!chainIdHex) return chains.mainnet;

const chainId = fromHex(chainIdHex as Hex, "number");
for (const chain of Object.values(chains)) {
if ("id" in chain) {
if (chain.id === chainId) {
return chain;
}
}
}

return chains.mainnet;
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./playwright";
export * from "./privateKeyWalletRequest";
export * from "./installMockWallet";
export * from "./createWallet";
Loading

0 comments on commit dfe347f

Please sign in to comment.