From 1b41be1f5f83086305daa1845fe649cf1b7fe6d1 Mon Sep 17 00:00:00 2001 From: matstyler Date: Mon, 8 Apr 2024 21:22:13 +0200 Subject: [PATCH] fix: cleanup --- examples/new-dawn/test/e2e/00_mock.spec.ts | 24 ++--- release/src/index.ts | 22 ++--- wallets/metamask-mock/environment.d.ts | 11 ++- wallets/metamask-mock/src/index.ts | 6 +- wallets/metamask-mock/src/metamask-mock.ts | 96 +++++++++---------- wallets/metamask-mock/src/network/Network.ts | 18 ++-- .../metamask-mock/src/testWithMetaMaskMock.ts | 69 +++++++------ wallets/metamask-mock/src/utils/index.ts | 2 +- .../metamask-mock/src/utils/mockEthereum.ts | 11 +-- .../test/e2e/metamask/addNewAccount.spec.ts | 22 ++--- .../importWalletFromPrivateKey.spec.ts | 20 ++-- .../e2e/metamask/mock/mockEthereum.spec.ts | 32 +++---- .../test/e2e/metamask/switchAccount.spec.ts | 20 ++-- .../test/e2e/metamask/switchNetwork.spec.ts | 26 +++-- 14 files changed, 173 insertions(+), 206 deletions(-) diff --git a/examples/new-dawn/test/e2e/00_mock.spec.ts b/examples/new-dawn/test/e2e/00_mock.spec.ts index 774435d00..8fcb14679 100644 --- a/examples/new-dawn/test/e2e/00_mock.spec.ts +++ b/examples/new-dawn/test/e2e/00_mock.spec.ts @@ -1,20 +1,16 @@ -import { testWithMetaMaskMock, MetaMaskMock } from "@synthetixio/synpress"; +import { MetaMaskMock, testWithMetaMaskMock } from '@synthetixio/synpress' -const test = testWithMetaMaskMock; +const test = testWithMetaMaskMock -const { expect } = test; +const { expect } = test -test("should work to the MetaMask Test Dapp", async ({ page }) => { - const metamask = new MetaMaskMock(page); +test('should work to the MetaMask Test Dapp', async ({ page }) => { + const metamask = new MetaMaskMock(page) - await page.goto("/"); + await page.goto('/') - await expect(page.locator("#accounts")).toHaveText( - "0xd73b04b0e696b0945283defa3eee453814758f1a" - ); + await expect(page.locator('#accounts')).toHaveText('0xd73b04b0e696b0945283defa3eee453814758f1a') - await page.locator("#getAccounts").click(); - await expect(page.locator("#getAccountsResult")).toHaveText( - "0xd73b04b0e696b0945283defa3eee453814758f1a" - ); -}); + await page.locator('#getAccounts').click() + await expect(page.locator('#getAccountsResult')).toHaveText('0xd73b04b0e696b0945283defa3eee453814758f1a') +}) diff --git a/release/src/index.ts b/release/src/index.ts index 59fd1c8a3..75bd9686d 100644 --- a/release/src/index.ts +++ b/release/src/index.ts @@ -1,17 +1,7 @@ -import { defineWalletSetup } from "@synthetixio/synpress-core"; -import { - getExtensionId, - testWithSynpress, -} from "@synthetixio/synpress-fixtures"; -import { - MetaMask, - homePageSelectors, - unlockForFixture, -} from "@synthetixio/synpress-metamask"; -import { - testWithMetaMaskMock, - MetaMaskMock, -} from "@synthetixio/synpress-metamask-mock"; +import { defineWalletSetup } from '@synthetixio/synpress-core' +import { getExtensionId, testWithSynpress } from '@synthetixio/synpress-fixtures' +import { MetaMask, homePageSelectors, unlockForFixture } from '@synthetixio/synpress-metamask' +import { MetaMaskMock, testWithMetaMaskMock } from '@synthetixio/synpress-metamask-mock' export { // Framework fixtures @@ -24,5 +14,5 @@ export { defineWalletSetup, getExtensionId, unlockForFixture, - homePageSelectors, -}; + homePageSelectors +} diff --git a/wallets/metamask-mock/environment.d.ts b/wallets/metamask-mock/environment.d.ts index 3da96a469..fed78f861 100644 --- a/wallets/metamask-mock/environment.d.ts +++ b/wallets/metamask-mock/environment.d.ts @@ -1,18 +1,19 @@ declare global { namespace NodeJS { interface ProcessEnv { - CI: boolean; - HEADLESS: boolean; + CI: boolean + HEADLESS: boolean } } } declare global { interface Window { - ethereum: any; + ethereum: import('ethers').Eip1193Provider } - const Web3Mock: any; + // biome-ignore lint/suspicious/noExplicitAny: Web3Mock is a mock object + const Web3Mock: any } -export {}; +export {} diff --git a/wallets/metamask-mock/src/index.ts b/wallets/metamask-mock/src/index.ts index 1377df821..94d5183c1 100644 --- a/wallets/metamask-mock/src/index.ts +++ b/wallets/metamask-mock/src/index.ts @@ -1,3 +1,3 @@ -export * from "./metamask-mock"; -export * from "./utils"; -export * from "./testWithMetaMaskMock"; +export * from './metamask-mock' +export * from './utils' +export * from './testWithMetaMaskMock' diff --git a/wallets/metamask-mock/src/metamask-mock.ts b/wallets/metamask-mock/src/metamask-mock.ts index 6c1c1e0c4..798277a1c 100644 --- a/wallets/metamask-mock/src/metamask-mock.ts +++ b/wallets/metamask-mock/src/metamask-mock.ts @@ -1,30 +1,28 @@ -import { mnemonicToAccount, privateKeyToAccount } from "viem/accounts"; // @ts-ignore -import { connect } from "@depay/web3-mock"; -import type { Page } from "@playwright/test"; -import type { Network } from "./network/Network"; +import { connect } from '@depay/web3-mock' +import type { Page } from '@playwright/test' +import { mnemonicToAccount, privateKeyToAccount } from 'viem/accounts' +import type { Network } from './network/Network' -export const blockchain = "ethereum"; -export const wallet = "metamask"; +export const blockchain = 'ethereum' +export const wallet = 'metamask' const networkConfig = { blockchain, - wallet, -}; + wallet +} -const BSC_NETWORK_ID = "0x38"; +const BSC_NETWORK_ID = '0x38' -export const SEED_PHRASE = - "test test test test test test test test test test test junk"; +export const SEED_PHRASE = 'test test test test test test test test test test test junk' -export const PRIVATE_KEY = - "ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a"; +export const PRIVATE_KEY = 'ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a' /** * This class is the heart of Synpress's MetaMask API. */ export class MetaMaskMock { - seedPhrase = ""; + seedPhrase = '' constructor( /** @@ -32,7 +30,7 @@ export class MetaMaskMock { */ readonly page: Page ) { - this.page = page; + this.page = page } /** @@ -41,7 +39,7 @@ export class MetaMaskMock { * @param seedPhrase - The seed phrase to import. */ importWallet(seedPhrase: string) { - this.seedPhrase = seedPhrase; + this.seedPhrase = seedPhrase } /** @@ -49,31 +47,31 @@ export class MetaMaskMock { */ async getAllAccounts(): Promise { return this.page.evaluate(() => { - return window.ethereum.request({ method: "eth_requestAccounts" }); - }); + return window.ethereum.request({ method: 'eth_requestAccounts' }) + }) } /** * Adds a new account. This account is based on the initially imported seed phrase. */ async addNewAccount() { - const accounts = await this.getAllAccounts(); + const accounts = await this.getAllAccounts() const newAccount = mnemonicToAccount(this.seedPhrase, { - accountIndex: accounts.length, - }); + accountIndex: accounts.length + }) return this.page.evaluate( ([networkConfig, accounts]) => { return Web3Mock.mock({ ...networkConfig, accounts: { - return: accounts, - }, - }); + return: accounts + } + }) }, [networkConfig, [newAccount.address, ...accounts]] - ); + ) } /** @@ -82,19 +80,19 @@ export class MetaMaskMock { * @param privateKey - The private key to import. */ async importWalletFromPrivateKey(privateKey: `0x${string}`) { - const newAccount = privateKeyToAccount(privateKey); + const newAccount = privateKeyToAccount(privateKey) return this.page.evaluate( ([networkConfig, account]) => { return Web3Mock.mock({ ...networkConfig, accounts: { - return: account, - }, - }); + return: account + } + }) }, [networkConfig, [newAccount.address]] - ); + ) } /** @@ -109,12 +107,12 @@ export class MetaMaskMock { // @ts-ignore ...networkConfig, accounts: { - return: [accountAddress], - }, - }); + return: [accountAddress] + } + }) }, [networkConfig, accountAddress] - ); + ) } /** @@ -133,27 +131,27 @@ export class MetaMaskMock { chainName: network.name, nativeCurrency: network.nativeCurrency, rpcUrls: [network.rpcUrl], - blockExplorerUrls: [network.blockExplorerUrl], - }; + blockExplorerUrls: [network.blockExplorerUrl] + } return this.page.evaluate( ([networkConfig, networkInfo]) => { return Web3Mock.mock({ ...networkConfig, network: { - add: networkInfo, - }, - }); + add: networkInfo + } + }) }, [networkConfig, networkInfo] - ); + ) } /** * Retrieves the current account address. */ async getAccountAddress() { - return (await this.getAllAccounts())[0]; + return (await this.getAllAccounts())[0] } /** @@ -168,24 +166,24 @@ export class MetaMaskMock { // @ts-ignore ...networkConfig, network: { - switchTo: networkName, - }, - }); + switchTo: networkName + } + }) window.ethereum.request({ - method: "wallet_switchEthereumChain", + method: 'wallet_switchEthereumChain', // Mock do not support custom network IDs - params: [{ chainId }], - }); + params: [{ chainId }] + }) }, [networkConfig, networkName, BSC_NETWORK_ID] - ); + ) } /** * Connects to the dapp using the currently selected account. */ async connectToDapp(network: string) { - connect(network); + connect(network) } } diff --git a/wallets/metamask-mock/src/network/Network.ts b/wallets/metamask-mock/src/network/Network.ts index cbed8bb09..23b839e84 100644 --- a/wallets/metamask-mock/src/network/Network.ts +++ b/wallets/metamask-mock/src/network/Network.ts @@ -1,11 +1,11 @@ export type Network = { - name: string; - rpcUrl: string; - chainId: number; - blockExplorerUrl?: string; + name: string + rpcUrl: string + chainId: number + blockExplorerUrl?: string nativeCurrency?: { - name: string; - symbol: string; - decimals: number; - }; -}; + name: string + symbol: string + decimals: number + } +} diff --git a/wallets/metamask-mock/src/testWithMetaMaskMock.ts b/wallets/metamask-mock/src/testWithMetaMaskMock.ts index 6495a0053..f6bd28f93 100644 --- a/wallets/metamask-mock/src/testWithMetaMaskMock.ts +++ b/wallets/metamask-mock/src/testWithMetaMaskMock.ts @@ -1,64 +1,59 @@ -import { readFileSync } from "fs"; -import { test as base } from "@playwright/test"; -import { MetaMaskMock, SEED_PHRASE } from "./metamask-mock"; -import type { Network } from "./network/Network"; -import { mockEthereum } from "./utils"; -import { createRequire } from "node:module"; +import { readFileSync } from 'fs' +import { createRequire } from 'node:module' +import { test as base } from '@playwright/test' +import { MetaMaskMock, SEED_PHRASE } from './metamask-mock' +import type { Network } from './network/Network' +import { mockEthereum } from './utils' -const ANVIL_CHAIN_ID = 31337; -const ANVIL_URL_URL = "http://anvil:5000"; +const ANVIL_CHAIN_ID = 31337 +const ANVIL_URL_URL = 'http://anvil:5000' -const require = createRequire(import.meta.url); +const require = createRequire(import.meta.url) // Relative path to the web3-mock bundle -const web3MockPath = require.resolve( - "@depay/web3-mock/dist/umd/index.bundle.js" -); +const web3MockPath = require.resolve('@depay/web3-mock/dist/umd/index.bundle.js') export const testWithMetaMaskMock = base.extend<{ - metamask: MetaMaskMock; - createAnvilNetwork: () => Network; - deployToken: () => Promise; + metamask: MetaMaskMock + createAnvilNetwork: () => Network + deployToken: () => Promise }>({ context: async ({ context }, use) => { // Dependency and mock function has to be added at the same time - https://playwright.dev/docs/api/class-browsercontext#browser-context-add-init-script await context.addInitScript({ - content: - readFileSync(web3MockPath, "utf-8") + - "\n" + - `(${mockEthereum.toString()})();`, - }); + content: `${readFileSync(web3MockPath, 'utf-8')}\n(${mockEthereum.toString()})();` + }) - await use(context); + await use(context) - await context.close(); + await context.close() }, page: async ({ context }, use) => { - const page = await context.newPage(); + const page = await context.newPage() - await page.goto("/"); + await page.goto('/') - await use(page); + await use(page) }, metamask: async ({ page }, use) => { - const metamask = new MetaMaskMock(page); + const metamask = new MetaMaskMock(page) - metamask.importWallet(SEED_PHRASE); + metamask.importWallet(SEED_PHRASE) - await use(metamask); + await use(metamask) }, createAnvilNetwork: async ({ context: _ }, use) => { await use(() => { return { - name: "Anvil", + name: 'Anvil', rpcUrl: ANVIL_URL_URL, chainId: ANVIL_CHAIN_ID, - blockExplorerUrl: "https://etherscan.io/", + blockExplorerUrl: 'https://etherscan.io/', nativeCurrency: { decimals: 18, - name: "Anvil", - symbol: "ETH", - }, - }; - }); - }, -}); + name: 'Anvil', + symbol: 'ETH' + } + } + }) + } +}) diff --git a/wallets/metamask-mock/src/utils/index.ts b/wallets/metamask-mock/src/utils/index.ts index 92fd67110..8fe4e82ee 100644 --- a/wallets/metamask-mock/src/utils/index.ts +++ b/wallets/metamask-mock/src/utils/index.ts @@ -1 +1 @@ -export { default as mockEthereum } from "./mockEthereum"; +export { default as mockEthereum } from './mockEthereum' diff --git a/wallets/metamask-mock/src/utils/mockEthereum.ts b/wallets/metamask-mock/src/utils/mockEthereum.ts index db665f526..5c500f5f7 100644 --- a/wallets/metamask-mock/src/utils/mockEthereum.ts +++ b/wallets/metamask-mock/src/utils/mockEthereum.ts @@ -1,10 +1,9 @@ export default function mockEthereum() { - // @ts-ignore Web3Mock.mock({ - blockchain: "ethereum", - wallet: "metamask", + blockchain: 'ethereum', + wallet: 'metamask', accounts: { - return: ["0xd73b04b0e696b0945283defa3eee453814758f1a"], - }, - }); + return: ['0xd73b04b0e696b0945283defa3eee453814758f1a'] + } + }) } diff --git a/wallets/metamask-mock/test/e2e/metamask/addNewAccount.spec.ts b/wallets/metamask-mock/test/e2e/metamask/addNewAccount.spec.ts index 2e6c4bfc7..66d501321 100644 --- a/wallets/metamask-mock/test/e2e/metamask/addNewAccount.spec.ts +++ b/wallets/metamask-mock/test/e2e/metamask/addNewAccount.spec.ts @@ -1,18 +1,18 @@ -import { testWithMetaMaskMock } from "../../../src"; +import { testWithMetaMaskMock } from '../../../src' -const test = testWithMetaMaskMock; +const test = testWithMetaMaskMock -const { expect } = test; +const { expect } = test -test("should add a new account with specified name", async ({ metamask }) => { - expect(await metamask.getAllAccounts()).toHaveLength(1); +test('should add a new account with specified name', async ({ metamask }) => { + expect(await metamask.getAllAccounts()).toHaveLength(1) - await metamask.addNewAccount(); + await metamask.addNewAccount() - expect(await metamask.getAllAccounts()).toHaveLength(2); + expect(await metamask.getAllAccounts()).toHaveLength(2) - await metamask.addNewAccount(); - await metamask.addNewAccount(); + await metamask.addNewAccount() + await metamask.addNewAccount() - expect(await metamask.getAllAccounts()).toHaveLength(4); -}); + expect(await metamask.getAllAccounts()).toHaveLength(4) +}) diff --git a/wallets/metamask-mock/test/e2e/metamask/importWalletFromPrivateKey.spec.ts b/wallets/metamask-mock/test/e2e/metamask/importWalletFromPrivateKey.spec.ts index 620f3abee..8d699d21c 100644 --- a/wallets/metamask-mock/test/e2e/metamask/importWalletFromPrivateKey.spec.ts +++ b/wallets/metamask-mock/test/e2e/metamask/importWalletFromPrivateKey.spec.ts @@ -1,16 +1,12 @@ -import { testWithMetaMaskMock } from "../../../src"; +import { testWithMetaMaskMock } from '../../../src' -const test = testWithMetaMaskMock; +const test = testWithMetaMaskMock -const { expect } = test; +const { expect } = test -test("should import account using private key", async ({ page, metamask }) => { - await metamask.importWalletFromPrivateKey( - "0xea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a" - ); +test('should import account using private key', async ({ page, metamask }) => { + await metamask.importWalletFromPrivateKey('0xea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a') - await page.locator("#getAccounts").click(); - await expect(page.locator("#getAccountsResult")).toHaveText( - "0xa2ce797cA71d0EaE1be5a7EffD27Fd6C38126801" - ); -}); + await page.locator('#getAccounts').click() + await expect(page.locator('#getAccountsResult')).toHaveText('0xa2ce797cA71d0EaE1be5a7EffD27Fd6C38126801') +}) diff --git a/wallets/metamask-mock/test/e2e/metamask/mock/mockEthereum.spec.ts b/wallets/metamask-mock/test/e2e/metamask/mock/mockEthereum.spec.ts index 8790f3e99..70fb1be59 100644 --- a/wallets/metamask-mock/test/e2e/metamask/mock/mockEthereum.spec.ts +++ b/wallets/metamask-mock/test/e2e/metamask/mock/mockEthereum.spec.ts @@ -1,25 +1,25 @@ -import { testWithMetaMaskMock } from "../../../../src"; +import { testWithMetaMaskMock } from '../../../../src' -const test = testWithMetaMaskMock; +const test = testWithMetaMaskMock -const { expect } = test; +const { expect } = test -test("should be able to access ethereum API", async ({ page }) => { - const ethereum = await page.evaluate(() => window.ethereum); - expect(ethereum).toBeTruthy(); -}); +test('should be able to access ethereum API', async ({ page }) => { + const ethereum = await page.evaluate(() => window.ethereum) + expect(ethereum).toBeTruthy() +}) -test("should be connected to metamask", async ({ page }) => { - const ethereum = await page.evaluate(() => window.ethereum); - expect(ethereum.isMetaMask).toBe(true); -}); +test('should be connected to metamask', async ({ page }) => { + const ethereum = await page.evaluate(() => window.ethereum) + expect(ethereum.isMetaMask).toBe(true) +}) -test("should connect to ethereum", async ({ page }) => { +test('should connect to ethereum', async ({ page }) => { const currentChainId = await page.evaluate(() => window.ethereum.request({ - method: "eth_chainId", + method: 'eth_chainId' }) - ); + ) - expect(currentChainId).toEqual("0x1"); -}); + expect(currentChainId).toEqual('0x1') +}) diff --git a/wallets/metamask-mock/test/e2e/metamask/switchAccount.spec.ts b/wallets/metamask-mock/test/e2e/metamask/switchAccount.spec.ts index 1e3f2fe1e..b3570e38d 100644 --- a/wallets/metamask-mock/test/e2e/metamask/switchAccount.spec.ts +++ b/wallets/metamask-mock/test/e2e/metamask/switchAccount.spec.ts @@ -1,16 +1,12 @@ -import { testWithMetaMaskMock } from "../../../src"; +import { testWithMetaMaskMock } from '../../../src' -const test = testWithMetaMaskMock; +const test = testWithMetaMaskMock -const { expect } = test; +const { expect } = test -test("should switch account", async ({ page, metamask }) => { - await metamask.switchAccount( - "0x4444797cA71d0EaE1be5a7EffD27Fd6C38126801" - ); +test('should switch account', async ({ page, metamask }) => { + await metamask.switchAccount('0x4444797cA71d0EaE1be5a7EffD27Fd6C38126801') - await page.locator("#getAccounts").click(); - await expect(page.locator("#getAccountsResult")).toHaveText( - "0x4444797cA71d0EaE1be5a7EffD27Fd6C38126801" - ); -}); + await page.locator('#getAccounts').click() + await expect(page.locator('#getAccountsResult')).toHaveText('0x4444797cA71d0EaE1be5a7EffD27Fd6C38126801') +}) diff --git a/wallets/metamask-mock/test/e2e/metamask/switchNetwork.spec.ts b/wallets/metamask-mock/test/e2e/metamask/switchNetwork.spec.ts index 70dbf7e3c..d04a2f238 100644 --- a/wallets/metamask-mock/test/e2e/metamask/switchNetwork.spec.ts +++ b/wallets/metamask-mock/test/e2e/metamask/switchNetwork.spec.ts @@ -1,23 +1,19 @@ -import { testWithMetaMaskMock } from "../../../src"; +import { testWithMetaMaskMock } from '../../../src' -const test = testWithMetaMaskMock; +const test = testWithMetaMaskMock -const { expect } = test; +const { expect } = test -test("should switch network", async ({ - createAnvilNetwork, - metamask, - page, -}) => { - const network = createAnvilNetwork(); +test('should switch network', async ({ createAnvilNetwork, metamask, page }) => { + const network = createAnvilNetwork() - await metamask.addNetwork(network); + await metamask.addNetwork(network) - await metamask.switchNetwork(network.name); + await metamask.switchNetwork(network.name) const chainId = await page.evaluate(async () => { - return await window.ethereum.request({ method: "eth_chainId" }); - }); + return await window.ethereum.request({ method: 'eth_chainId' }) + }) - expect(chainId).toBe("0x38"); -}); + expect(chainId).toBe('0x38') +})