test: upgrade cypress-hardhat (#6462)
This commit is contained in:
parent
803485b96a
commit
3b1ef8033b
@ -1,10 +1,8 @@
|
|||||||
import { USDC_MAINNET } from '../../src/constants/tokens'
|
import { USDC_MAINNET } from '../../src/constants/tokens'
|
||||||
import { WETH_GOERLI } from '../fixtures/constants'
|
import { WETH_GOERLI } from '../fixtures/constants'
|
||||||
import { HardhatProvider } from '../support/hardhat'
|
|
||||||
import { getTestSelector } from '../utils'
|
import { getTestSelector } from '../utils'
|
||||||
|
|
||||||
describe('Swap', () => {
|
describe('Swap', () => {
|
||||||
let hardhat: HardhatProvider
|
|
||||||
const verifyAmount = (field: 'input' | 'output', amountText: string | null) => {
|
const verifyAmount = (field: 'input' | 'output', amountText: string | null) => {
|
||||||
if (amountText === null) {
|
if (amountText === null) {
|
||||||
cy.get(`#swap-currency-${field} .token-amount-input`).should('not.have.value')
|
cy.get(`#swap-currency-${field} .token-amount-input`).should('not.have.value')
|
||||||
@ -46,9 +44,7 @@ describe('Swap', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
before(() => {
|
before(() => {
|
||||||
cy.visit('/swap', { ethereum: 'hardhat' }).then((window) => {
|
cy.visit('/swap', { ethereum: 'hardhat' })
|
||||||
hardhat = window.hardhat
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('starts with ETH selected by default', () => {
|
it('starts with ETH selected by default', () => {
|
||||||
@ -81,35 +77,33 @@ describe('Swap', () => {
|
|||||||
it('can swap ETH for USDC', () => {
|
it('can swap ETH for USDC', () => {
|
||||||
const TOKEN_ADDRESS = USDC_MAINNET.address
|
const TOKEN_ADDRESS = USDC_MAINNET.address
|
||||||
const BALANCE_INCREMENT = 1
|
const BALANCE_INCREMENT = 1
|
||||||
cy.visit('/swap', { ethereum: 'hardhat' })
|
cy.hardhat().then((hardhat) => {
|
||||||
.then((window) => {
|
cy.then(() => hardhat.getBalance(hardhat.wallet.address, USDC_MAINNET))
|
||||||
hardhat = window.hardhat
|
.then((balance) => Number(balance.toFixed(1)))
|
||||||
})
|
.then((initialBalance) => {
|
||||||
.then(() => hardhat.utils.getBalance(hardhat.wallet.address, USDC_MAINNET))
|
cy.get('#swap-currency-output .open-currency-select-button').click()
|
||||||
.then((balance) => Number(balance.toFixed(1)))
|
cy.get(getTestSelector('token-search-input')).clear().type(TOKEN_ADDRESS)
|
||||||
.then((initialBalance) => {
|
cy.contains('USDC').click()
|
||||||
cy.get('#swap-currency-output .open-currency-select-button').click()
|
cy.get('#swap-currency-output .token-amount-input').clear().type(BALANCE_INCREMENT.toString())
|
||||||
cy.get(getTestSelector('token-search-input')).clear().type(TOKEN_ADDRESS)
|
cy.get('#swap-currency-input .token-amount-input').should('not.equal', '')
|
||||||
cy.contains('USDC').click()
|
cy.get('#swap-button').click()
|
||||||
cy.get('#swap-currency-output .token-amount-input').clear().type(BALANCE_INCREMENT.toString())
|
cy.get('#confirm-swap-or-send').click()
|
||||||
cy.get('#swap-currency-input .token-amount-input').should('not.equal', '')
|
cy.get(getTestSelector('dismiss-tx-confirmation')).click()
|
||||||
cy.get('#swap-button').click()
|
|
||||||
cy.get('#confirm-swap-or-send').click()
|
|
||||||
cy.get(getTestSelector('dismiss-tx-confirmation')).click()
|
|
||||||
|
|
||||||
cy.then(() => hardhat.send('hardhat_mine', ['0x1', '0xc'])).then(() => {
|
cy.then(() => hardhat.provider.send('hardhat_mine', ['0x1', '0xc'])).then(() => {
|
||||||
// ui check
|
// ui check
|
||||||
cy.get('#swap-currency-output [data-testid="balance-text"]').should(
|
cy.get('#swap-currency-output [data-testid="balance-text"]').should(
|
||||||
'have.text',
|
'have.text',
|
||||||
`Balance: ${initialBalance + BALANCE_INCREMENT}`
|
`Balance: ${initialBalance + BALANCE_INCREMENT}`
|
||||||
)
|
)
|
||||||
|
|
||||||
// chain state check
|
// chain state check
|
||||||
cy.then(() => hardhat.utils.getBalance(hardhat.wallet.address, USDC_MAINNET))
|
cy.then(() => hardhat.getBalance(hardhat.wallet.address, USDC_MAINNET))
|
||||||
.then((balance) => Number(balance.toFixed(1)))
|
.then((balance) => Number(balance.toFixed(1)))
|
||||||
.should('eq', initialBalance + BALANCE_INCREMENT)
|
.should('eq', initialBalance + BALANCE_INCREMENT)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should have the correct default input/output and token selection should work', () => {
|
it('should have the correct default input/output and token selection should work', () => {
|
||||||
@ -162,21 +156,22 @@ describe('Swap', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should render and dismiss the wallet rejection modal', () => {
|
it('should render and dismiss the wallet rejection modal', () => {
|
||||||
cy.visit('/swap', { ethereum: 'hardhat' }).then((window) => {
|
cy.visit('/swap', { ethereum: 'hardhat' })
|
||||||
hardhat = window.hardhat
|
.hardhat()
|
||||||
cy.stub(hardhat.wallet, 'sendTransaction').rejects(new Error('user cancelled'))
|
.then((hardhat) => {
|
||||||
|
cy.stub(hardhat.wallet, 'sendTransaction').rejects(new Error('user cancelled'))
|
||||||
|
|
||||||
cy.get('#swap-currency-output .open-currency-select-button').click()
|
cy.get('#swap-currency-output .open-currency-select-button').click()
|
||||||
cy.get(getTestSelector('token-search-input')).clear().type(USDC_MAINNET.address)
|
cy.get(getTestSelector('token-search-input')).clear().type(USDC_MAINNET.address)
|
||||||
cy.contains('USDC').click()
|
cy.contains('USDC').click()
|
||||||
cy.get('#swap-currency-output .token-amount-input').clear().type('1')
|
cy.get('#swap-currency-output .token-amount-input').clear().type('1')
|
||||||
cy.get('#swap-currency-input .token-amount-input').should('not.equal', '')
|
cy.get('#swap-currency-input .token-amount-input').should('not.equal', '')
|
||||||
cy.get('#swap-button').click()
|
cy.get('#swap-button').click()
|
||||||
cy.get('#confirm-swap-or-send').click()
|
cy.get('#confirm-swap-or-send').click()
|
||||||
cy.contains('Transaction rejected').should('exist')
|
cy.contains('Transaction rejected').should('exist')
|
||||||
cy.contains('Dismiss').click()
|
cy.contains('Dismiss').click()
|
||||||
cy.contains('Transaction rejected').should('not.exist')
|
cy.contains('Transaction rejected').should('not.exist')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Opens and closes the settings menu', () => {
|
it('Opens and closes the settings menu', () => {
|
||||||
|
@ -6,23 +6,21 @@
|
|||||||
// ***********************************************************
|
// ***********************************************************
|
||||||
|
|
||||||
import '@cypress/code-coverage/support'
|
import '@cypress/code-coverage/support'
|
||||||
|
import 'cypress-hardhat/lib/browser'
|
||||||
|
|
||||||
import { Eip1193Bridge } from '@ethersproject/experimental/lib/eip1193-bridge'
|
import { Eip1193Bridge } from '@ethersproject/experimental/lib/eip1193-bridge'
|
||||||
import assert from 'assert'
|
import assert from 'assert'
|
||||||
import { Network } from 'cypress-hardhat/lib/browser'
|
|
||||||
|
|
||||||
import { FeatureFlag } from '../../src/featureFlags/flags/featureFlags'
|
import { FeatureFlag } from '../../src/featureFlags/flags/featureFlags'
|
||||||
import { UserState } from '../../src/state/user/reducer'
|
import { UserState } from '../../src/state/user/reducer'
|
||||||
import { CONNECTED_WALLET_USER_STATE } from '../utils/user-state'
|
import { CONNECTED_WALLET_USER_STATE } from '../utils/user-state'
|
||||||
import { injected } from './ethereum'
|
import { injected } from './ethereum'
|
||||||
import { HardhatProvider } from './hardhat'
|
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-namespace
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||||
namespace Cypress {
|
namespace Cypress {
|
||||||
interface ApplicationWindow {
|
interface ApplicationWindow {
|
||||||
ethereum: Eip1193Bridge
|
ethereum: Eip1193Bridge
|
||||||
hardhat: HardhatProvider
|
|
||||||
}
|
}
|
||||||
interface VisitOptions {
|
interface VisitOptions {
|
||||||
serviceWorker?: true
|
serviceWorker?: true
|
||||||
@ -39,10 +37,6 @@ declare global {
|
|||||||
*/
|
*/
|
||||||
userState?: Partial<UserState>
|
userState?: Partial<UserState>
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
interface Chainable<Subject> {
|
|
||||||
task(event: 'hardhat'): Chainable<Network>
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,8 +53,8 @@ Cypress.Commands.overwrite(
|
|||||||
|
|
||||||
return cy
|
return cy
|
||||||
.intercept('/service-worker.js', options?.serviceWorker ? undefined : { statusCode: 404 })
|
.intercept('/service-worker.js', options?.serviceWorker ? undefined : { statusCode: 404 })
|
||||||
.task('hardhat')
|
.provider()
|
||||||
.then((network) =>
|
.then((provider) =>
|
||||||
original({
|
original({
|
||||||
...options,
|
...options,
|
||||||
url: hashUrl,
|
url: hashUrl,
|
||||||
@ -84,9 +78,7 @@ Cypress.Commands.overwrite(
|
|||||||
|
|
||||||
// Inject the mock ethereum provider.
|
// Inject the mock ethereum provider.
|
||||||
if (options?.ethereum === 'hardhat') {
|
if (options?.ethereum === 'hardhat') {
|
||||||
// The provider is exposed via hardhat to allow mocking / network manipulation.
|
win.ethereum = provider
|
||||||
win.hardhat = new HardhatProvider(network)
|
|
||||||
win.ethereum = win.hardhat
|
|
||||||
} else {
|
} else {
|
||||||
win.ethereum = injected
|
win.ethereum = injected
|
||||||
}
|
}
|
||||||
|
@ -1,81 +0,0 @@
|
|||||||
import { Eip1193Bridge } from '@ethersproject/experimental/lib/eip1193-bridge'
|
|
||||||
import { JsonRpcProvider } from '@ethersproject/providers'
|
|
||||||
import { Wallet } from '@ethersproject/wallet'
|
|
||||||
import { HardhatUtils, Network } from 'cypress-hardhat/lib/browser'
|
|
||||||
|
|
||||||
export class HardhatProvider extends Eip1193Bridge {
|
|
||||||
readonly utils: HardhatUtils
|
|
||||||
readonly chainId: string
|
|
||||||
readonly wallet: Wallet
|
|
||||||
|
|
||||||
isMetaMask = true
|
|
||||||
|
|
||||||
constructor(network: Network) {
|
|
||||||
const utils = new HardhatUtils(network)
|
|
||||||
const wallet = new Wallet(utils.account.privateKey, utils.provider)
|
|
||||||
super(wallet, utils.provider)
|
|
||||||
|
|
||||||
this.utils = utils
|
|
||||||
this.chainId = `0x${network.chainId.toString(16)}`
|
|
||||||
this.wallet = wallet
|
|
||||||
}
|
|
||||||
|
|
||||||
async sendAsync(...args: any[]) {
|
|
||||||
return this.send(...args)
|
|
||||||
}
|
|
||||||
|
|
||||||
async send(...args: any[]) {
|
|
||||||
console.debug('hardhat:send', ...args)
|
|
||||||
|
|
||||||
// Parse callback form.
|
|
||||||
const isCallbackForm = typeof args[0] === 'object' && typeof args[1] === 'function'
|
|
||||||
let callback = <T>(error: Error | null, result?: { result: T }) => {
|
|
||||||
if (error) throw error
|
|
||||||
return result?.result
|
|
||||||
}
|
|
||||||
let method
|
|
||||||
let params
|
|
||||||
if (isCallbackForm) {
|
|
||||||
callback = args[1]
|
|
||||||
method = args[0].method
|
|
||||||
params = args[0].params
|
|
||||||
} else {
|
|
||||||
method = args[0]
|
|
||||||
params = args[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
let result
|
|
||||||
try {
|
|
||||||
switch (method) {
|
|
||||||
case 'eth_requestAccounts':
|
|
||||||
case 'eth_accounts':
|
|
||||||
result = [this.wallet.address]
|
|
||||||
break
|
|
||||||
case 'eth_chainId':
|
|
||||||
result = this.chainId
|
|
||||||
break
|
|
||||||
case 'eth_sendTransaction': {
|
|
||||||
// Eip1193Bridge doesn't support .gas and .from directly, so we massage it to satisfy ethers' expectations.
|
|
||||||
// See https://github.com/ethers-io/ethers.js/issues/1683.
|
|
||||||
params[0].gasLimit = params[0].gas
|
|
||||||
delete params[0].gas
|
|
||||||
delete params[0].from
|
|
||||||
|
|
||||||
const req = JsonRpcProvider.hexlifyTransaction(params[0])
|
|
||||||
req.gasLimit = req.gas
|
|
||||||
delete req.gas
|
|
||||||
|
|
||||||
result = (await this.signer.sendTransaction(req)).hash
|
|
||||||
break
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
result = await super.send(method, params)
|
|
||||||
}
|
|
||||||
console.debug('hardhat:receive', method, result)
|
|
||||||
return callback(null, { result })
|
|
||||||
} catch (error) {
|
|
||||||
console.debug('hardhat:error', method, error)
|
|
||||||
return callback(error as Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -103,7 +103,7 @@
|
|||||||
"@vanilla-extract/webpack-plugin": "^2.1.11",
|
"@vanilla-extract/webpack-plugin": "^2.1.11",
|
||||||
"babel-plugin-istanbul": "^6.1.1",
|
"babel-plugin-istanbul": "^6.1.1",
|
||||||
"cypress": "10.3.1",
|
"cypress": "10.3.1",
|
||||||
"cypress-hardhat": "^1.0.1",
|
"cypress-hardhat": "^2.0.0",
|
||||||
"env-cmd": "^10.1.0",
|
"env-cmd": "^10.1.0",
|
||||||
"eslint": "^7.11.0",
|
"eslint": "^7.11.0",
|
||||||
"eslint-plugin-import": "^2.27",
|
"eslint-plugin-import": "^2.27",
|
||||||
|
@ -9046,10 +9046,10 @@ cyclist@^1.0.1:
|
|||||||
resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9"
|
resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9"
|
||||||
integrity sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=
|
integrity sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=
|
||||||
|
|
||||||
cypress-hardhat@^1.0.1:
|
cypress-hardhat@^2.0.0:
|
||||||
version "1.0.1"
|
version "2.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/cypress-hardhat/-/cypress-hardhat-1.0.1.tgz#11b86653282769dadc0bd0c65ca41011865b0762"
|
resolved "https://registry.yarnpkg.com/cypress-hardhat/-/cypress-hardhat-2.0.0.tgz#98f07370270ec7c754d35f77c73b216d9f924abb"
|
||||||
integrity sha512-eGD7fNM8BXShXEsDbO/m2jv9mx7jHs44bnuWKYxO29ySXX5Soz9+AFYelhzKDvh/T+MJy1YqApPbif9+PNA++g==
|
integrity sha512-YLLVZa/15CBo7mmu5JuIGAPg4jLbSYTw/LR690tx90WEiIaPH9diHzWAE41wB+cvmgM7fGwhKnKEEeE1s1DWKg==
|
||||||
|
|
||||||
cypress@*, cypress@10.3.1:
|
cypress@*, cypress@10.3.1:
|
||||||
version "10.3.1"
|
version "10.3.1"
|
||||||
|
Loading…
Reference in New Issue
Block a user