Skip to content

Commit

Permalink
✅ feat: Added Cypress example (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
matstyler authored Jun 19, 2024
1 parent df5b9aa commit 9e8ac48
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 50 deletions.
15 changes: 15 additions & 0 deletions examples/new-dawn/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { installSynpress } from '@synthetixio/synpress/cypress'
import { defineConfig } from 'cypress'

export default defineConfig({
chromeWebSecurity: false,
e2e: {
baseUrl: 'http://localhost:9999',
specPattern: 'test/cypress/**/*.cy.{js,jsx,ts,tsx}',
supportFile: 'test/cypress/support/e2e.{js,jsx,ts,tsx}',
testIsolation: false,
async setupNodeEvents(on, config) {
return installSynpress(on, config)
}
}
})
2 changes: 2 additions & 0 deletions examples/new-dawn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build:cache:headless": "synpress --headless",
"serve:test-dapp": "serve node_modules/@metamask/test-dapp/dist -p 9999",
"test:e2e:headful": "playwright test",
"test:e2e:headful:cypress": "cypress run --browser chrome --headed",
"test:e2e:headless": "HEADLESS=true playwright test",
"test:e2e:headless:ui": "HEADLESS=true playwright test --ui"
},
Expand All @@ -20,6 +21,7 @@
"devDependencies": {
"@metamask/test-dapp": "8.1.0",
"@types/node": "20.11.17",
"cypress": "13.9.0",
"serve": "14.2.1",
"typescript": "5.3.3"
}
Expand Down
15 changes: 15 additions & 0 deletions examples/new-dawn/test/cypress/00_mock.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
it('should mock MetaMask in the Test Dapp', () => {
cy.connectToDapp()

cy.get('#getAccounts').click()

cy.get('#getAccountsResult').should('have.text', '0xd73b04b0e696b0945283defa3eee453814758f1a')
})

it('should add new account using MetaMask mock', () => {
cy.switchAccount('0x4444797cA71d0EaE1be5a7EffD27Fd6C38126801')

cy.get('#getAccounts').click()

cy.get('#getAccountsResult').should('have.text', '0x4444797cA71d0EaE1be5a7EffD27Fd6C38126801')
})
25 changes: 25 additions & 0 deletions examples/new-dawn/test/cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************************
// This example support/e2e.ts is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import { mockEthereum, synpressCommands } from '@synthetixio/synpress/cypress/support'

synpressCommands()
mockEthereum()

Cypress.on('uncaught:exception', () => {
// failing the test
return false
})
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

4 changes: 4 additions & 0 deletions release/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"types": "./types/cypress/index.d.ts",
"default": "./dist/cypress/index.js"
},
"./cypress/support": {
"types": "./types/cypress/support.d.ts",
"default": "./dist/cypress/support.js"
},
"./playwright": {
"types": "./types/playwright/index.d.ts",
"default": "./dist/playwright/index.js"
Expand Down
1 change: 1 addition & 0 deletions release/src/cypress/support.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@synthetixio/ethereum-wallet-mock/cypress/support'
2 changes: 1 addition & 1 deletion release/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig } from 'tsup'

export default defineConfig({
name: 'synpress',
entry: ['src/index.ts', 'src/cli.ts', 'src/cypress/index.ts', 'src/playwright/index.ts'],
entry: ['src/index.ts', 'src/cli.ts', 'src/cypress/index.ts', 'src/cypress/support.ts', 'src/playwright/index.ts'],
outDir: 'dist',
format: 'esm',
splitting: false,
Expand Down
4 changes: 4 additions & 0 deletions wallets/ethereum-wallet-mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"types": "./types/cypress/index.d.ts",
"default": "./dist/cypress/index.js"
},
"./cypress/support": {
"types": "./types/cypress/support/index.d.ts",
"default": "./dist/cypress/support/index.js"
},
"./playwright": {
"types": "./types/playwright/index.d.ts",
"default": "./dist/playwright/index.js"
Expand Down
7 changes: 5 additions & 2 deletions wallets/ethereum-wallet-mock/src/cypress/support/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
// https://on.cypress.io/configuration
// ***********************************************************

import mockEthereum from './mockEthereum'
// Import commands.js using ES2015 syntax:
import './commands'
import './mockEthereum'
import synpressCommands from './synpressCommands'

synpressCommands()
mockEthereum()

Cypress.on('uncaught:exception', () => {
// failing the test
Expand Down
2 changes: 2 additions & 0 deletions wallets/ethereum-wallet-mock/src/cypress/support/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as mockEthereum } from './mockEthereum'
export { default as synpressCommands } from './synpressCommands'
20 changes: 11 additions & 9 deletions wallets/ethereum-wallet-mock/src/cypress/support/mockEthereum.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
before(() => {
cy.visit('/', {
onBeforeLoad: (window) => {
window.Web3Mock.mock({
blockchain: 'ethereum',
wallet: 'metamask'
})
}
export default function mockEthereum() {
before(() => {
cy.visit('/', {
onBeforeLoad: (window) => {
window.Web3Mock.mock({
blockchain: 'ethereum',
wallet: 'metamask'
})
}
})
})
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import type { Network } from '../../type/Network'
import type { WalletMock } from '../../type/WalletMock'
// import type { Network } from "../../type/Network";
import getEthereumWalletMock from '../utils/getEthereumWalletMock'

declare global {
Expand Down Expand Up @@ -54,48 +53,50 @@ declare global {
}
}

Cypress.Commands.add('importWallet', (seedPhrase) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.importWallet(seedPhrase)
})
export default function synpressCommands() {
Cypress.Commands.add('importWallet', (seedPhrase) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.importWallet(seedPhrase)
})

Cypress.Commands.add('importWalletFromPrivateKey', (privateKey) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.importWalletFromPrivateKey(privateKey)
})
Cypress.Commands.add('importWalletFromPrivateKey', (privateKey) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.importWalletFromPrivateKey(privateKey)
})

Cypress.Commands.add('addNewAccount', () => {
const ethereumWalletMock = getEthereumWalletMock()
return ethereumWalletMock.addNewAccount()
})
Cypress.Commands.add('addNewAccount', () => {
const ethereumWalletMock = getEthereumWalletMock()
return ethereumWalletMock.addNewAccount()
})

Cypress.Commands.add('getAllAccounts', () => {
const ethereumWalletMock = getEthereumWalletMock()
Cypress.Commands.add('getAllAccounts', () => {
const ethereumWalletMock = getEthereumWalletMock()

return ethereumWalletMock.getAllAccounts()
})
return ethereumWalletMock.getAllAccounts()
})

Cypress.Commands.add('getAccountAddress', () => {
const ethereumWalletMock = getEthereumWalletMock()
return ethereumWalletMock.getAccountAddress()
})
Cypress.Commands.add('getAccountAddress', () => {
const ethereumWalletMock = getEthereumWalletMock()
return ethereumWalletMock.getAccountAddress()
})

Cypress.Commands.add('switchAccount', (accountAddress) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.switchAccount(accountAddress)
})
Cypress.Commands.add('switchAccount', (accountAddress) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.switchAccount(accountAddress)
})

Cypress.Commands.add('addNetwork', (network) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.addNetwork(network)
})
Cypress.Commands.add('addNetwork', (network) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.addNetwork(network)
})

Cypress.Commands.add('switchNetwork', (networkName) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.switchNetwork(networkName)
})
Cypress.Commands.add('switchNetwork', (networkName) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.switchNetwork(networkName)
})

Cypress.Commands.add('connectToDapp', (wallet) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.connectToDapp(wallet)
})
Cypress.Commands.add('connectToDapp', (wallet) => {
const ethereumWalletMock = getEthereumWalletMock()
ethereumWalletMock.connectToDapp(wallet)
})
}
2 changes: 1 addition & 1 deletion wallets/ethereum-wallet-mock/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig } from 'tsup'

export default defineConfig({
name: 'ethereum-wallet-mock',
entry: ['src/index.ts', 'src/playwright/index.ts', 'src/cypress/index.ts'],
entry: ['src/index.ts', 'src/playwright/index.ts', 'src/cypress/index.ts', 'src/cypress/support/index.ts'],
outDir: 'dist',
format: 'esm',
splitting: false,
Expand Down

0 comments on commit 9e8ac48

Please sign in to comment.