Commit 2a50d6a1 authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

test(e2e): reset hardhat between tests (#6487)

parent c8a81491
...@@ -84,10 +84,9 @@ describe('Swap', () => { ...@@ -84,10 +84,9 @@ describe('Swap', () => {
const DEADLINE_MINUTES = 1 const DEADLINE_MINUTES = 1
const TEN_MINUTES_MS = 1000 * 60 * DEADLINE_MINUTES * 10 const TEN_MINUTES_MS = 1000 * 60 * DEADLINE_MINUTES * 10
cy.visit('/swap', { ethereum: 'hardhat' }) cy.visit('/swap', { ethereum: 'hardhat' })
.hardhat() .hardhat({ automine: false })
.then((hardhat) => { .then((hardhat) => {
cy.then(() => hardhat.setAutomine(false)) cy.then(() => hardhat.getBalance(hardhat.wallet.address, USDC_MAINNET))
.then(() => hardhat.getBalance(hardhat.wallet.address, USDC_MAINNET))
.then((balance) => Number(balance.toFixed(1))) .then((balance) => Number(balance.toFixed(1)))
.then((initialBalance) => { .then((initialBalance) => {
// Input swap info. // Input swap info.
...@@ -209,18 +208,16 @@ describe('Swap', () => { ...@@ -209,18 +208,16 @@ describe('Swap', () => {
cy.get('#confirm-swap-or-send').click() cy.get('#confirm-swap-or-send').click()
cy.get(getTestSelector('dismiss-tx-confirmation')).click() cy.get(getTestSelector('dismiss-tx-confirmation')).click()
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.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)
})
}) })
}) })
}) })
...@@ -417,68 +414,65 @@ describe('Swap', () => { ...@@ -417,68 +414,65 @@ describe('Swap', () => {
it('should render an error for slippage failure', () => { it('should render an error for slippage failure', () => {
cy.visit('/swap', { ethereum: 'hardhat' }) cy.visit('/swap', { ethereum: 'hardhat' })
.hardhat() .hardhat({ automine: false })
.then((hardhat) => { .then((hardhat) => {
cy.then(() => hardhat.setAutomine(false)) cy.then(() => hardhat.provider.getBalance(hardhat.wallet.address)).then((initialBalance) => {
.then(() => hardhat.provider.getBalance(hardhat.wallet.address)) // Gas estimation fails for this transaction (that would normally fail), so we stub it.
.then((initialBalance) => { const send = cy.stub(hardhat.provider, 'send')
// Gas estimation fails for this transaction (that would normally fail), so we stub it. send.withArgs('eth_estimateGas').resolves(BigNumber.from(2_000_000))
const send = cy.stub(hardhat.provider, 'send') send.callThrough()
send.withArgs('eth_estimateGas').resolves(BigNumber.from(2_000_000))
send.callThrough() // Set slippage to a very low value.
cy.get(getTestSelector('open-settings-dialog-button')).click()
// Set slippage to a very low value. cy.get(getTestSelector('slippage-input')).clear().type('0.01')
cy.get(getTestSelector('open-settings-dialog-button')).click() cy.get('body').click('topRight')
cy.get(getTestSelector('slippage-input')).clear().type('0.01') cy.get(getTestSelector('slippage-input')).should('not.exist')
cy.get('body').click('topRight')
cy.get(getTestSelector('slippage-input')).should('not.exist') // Open the currency select modal.
cy.get('#swap-currency-output .open-currency-select-button').click()
// Open the currency select modal.
cy.get('#swap-currency-output .open-currency-select-button').click() // Wait for the currency list to load
cy.contains('1inch').should('exist')
// Wait for the currency list to load
cy.contains('1inch').should('exist') // Select UNI as output token
cy.get(getTestSelector('token-search-input')).clear().type('Uniswap')
// Select UNI as output token cy.get(getTestSelector('currency-list-wrapper'))
cy.get(getTestSelector('token-search-input')).clear().type('Uniswap') .contains(/^Uniswap$/)
cy.get(getTestSelector('currency-list-wrapper')) .first()
.contains(/^Uniswap$/) .should('exist')
.first() .click()
.should('exist')
.click() // Swap 2 times.
const AMOUNT_TO_SWAP = 400
// Swap 2 times. const NUMBER_OF_SWAPS = 2
const AMOUNT_TO_SWAP = 400 const INDIVIDUAL_SWAP_INPUT = AMOUNT_TO_SWAP / NUMBER_OF_SWAPS
const NUMBER_OF_SWAPS = 2 cy.get('#swap-currency-input .token-amount-input').clear().type(INDIVIDUAL_SWAP_INPUT.toString())
const INDIVIDUAL_SWAP_INPUT = AMOUNT_TO_SWAP / NUMBER_OF_SWAPS cy.get('#swap-currency-output .token-amount-input').should('not.equal', '')
cy.get('#swap-currency-input .token-amount-input').clear().type(INDIVIDUAL_SWAP_INPUT.toString()) cy.get('#swap-button').click()
cy.get('#swap-currency-output .token-amount-input').should('not.equal', '') cy.get('#confirm-swap-or-send').click()
cy.get('#swap-button').click() cy.get(getTestSelector('dismiss-tx-confirmation')).click()
cy.get('#confirm-swap-or-send').click() cy.get('#swap-currency-input .token-amount-input').clear().type(INDIVIDUAL_SWAP_INPUT.toString())
cy.get(getTestSelector('dismiss-tx-confirmation')).click() cy.get('#swap-currency-output .token-amount-input').should('not.equal', '')
cy.get('#swap-currency-input .token-amount-input').clear().type(INDIVIDUAL_SWAP_INPUT.toString()) cy.get('#swap-button').click()
cy.get('#swap-currency-output .token-amount-input').should('not.equal', '') cy.get('#confirm-swap-or-send').click()
cy.get('#swap-button').click() cy.get(getTestSelector('dismiss-tx-confirmation')).click()
cy.get('#confirm-swap-or-send').click()
cy.get(getTestSelector('dismiss-tx-confirmation')).click() // The pending transaction indicator should be visible.
cy.contains('Pending').should('exist')
// The pending transaction indicator should be visible.
cy.contains('Pending').should('exist') cy.then(() => hardhat.mine()).then(() => {
// The pending transaction indicator should not be visible.
cy.then(() => hardhat.mine()).then(() => { cy.contains('Pending').should('not.exist')
// The pending transaction indicator should not be visible.
cy.contains('Pending').should('not.exist') // Check for a failed transaction notification.
cy.contains('Swap failed').should('exist')
// Check for a failed transaction notification.
cy.contains('Swap failed').should('exist') // Assert that at least one of the swaps failed due to slippage.
cy.then(() => hardhat.provider.getBalance(hardhat.wallet.address)).then((finalBalance) => {
// Assert that at least one of the swaps failed due to slippage. expect(finalBalance.gt(initialBalance.sub(parseEther(AMOUNT_TO_SWAP.toString())))).to.be.true
cy.then(() => hardhat.provider.getBalance(hardhat.wallet.address)).then((finalBalance) => {
expect(finalBalance.gt(initialBalance.sub(parseEther(AMOUNT_TO_SWAP.toString())))).to.be.true
})
}) })
}) })
.then(() => hardhat.setAutomine(true)) })
}) })
}) })
}) })
...@@ -89,11 +89,12 @@ Cypress.Commands.overwrite( ...@@ -89,11 +89,12 @@ Cypress.Commands.overwrite(
) )
beforeEach(() => { beforeEach(() => {
// Many API calls enforce that requests come from our app, so we must mock Origin and Referer. cy
cy.intercept('*', (req) => { // Many API calls enforce that requests come from our app, so we must mock Origin and Referer.
req.headers['referer'] = 'https://app.uniswap.org' .intercept('*', (req) => {
req.headers['origin'] = 'https://app.uniswap.org' req.headers['referer'] = 'https://app.uniswap.org'
}) req.headers['origin'] = 'https://app.uniswap.org'
})
// Infura uses a test endpoint, which allow-lists http://localhost:3000 instead. // Infura uses a test endpoint, which allow-lists http://localhost:3000 instead.
.intercept(/infura.io/, (req) => { .intercept(/infura.io/, (req) => {
req.headers['referer'] = 'http://localhost:3000' req.headers['referer'] = 'http://localhost:3000'
...@@ -114,6 +115,10 @@ beforeEach(() => { ...@@ -114,6 +115,10 @@ beforeEach(() => {
}) })
) )
}) })
// Reset hardhat between tests to ensure isolation.
// This resets the fork, as well as options like automine.
.hardhat()
.then((hardhat) => hardhat.reset())
}) })
Cypress.on('uncaught:exception', () => { Cypress.on('uncaught:exception', () => {
......
...@@ -22,8 +22,8 @@ module.exports = { ...@@ -22,8 +22,8 @@ module.exports = {
count: 1, count: 1,
}, },
mining: { mining: {
auto: true, auto: true, // automine to make tests easier to write.
interval: 0, interval: 0, // do not interval mine so that tests remain deterministic
}, },
}, },
}, },
......
...@@ -9046,10 +9046,10 @@ cyclist@^1.0.1: ...@@ -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@^2.2.0: cypress-hardhat@^2.3.0:
version "2.2.0" version "2.3.0"
resolved "https://registry.yarnpkg.com/cypress-hardhat/-/cypress-hardhat-2.2.0.tgz#4e1c0f16f0c915dda54ddeccbf5dea19f80eed2f" resolved "https://registry.yarnpkg.com/cypress-hardhat/-/cypress-hardhat-2.3.0.tgz#646b35d57490d060e3fd4441e76e4d91b4ff4ec7"
integrity sha512-Jtzd49vvyWSshR43w18WJ4b/40EhSItTTl82QYBNqiSPPrC/yj0derJHZyqMqq2LZvrsfmiY92fIBDgvxUCxZQ== integrity sha512-Sj437lFrUZ9UJGXS5a+DLQPBoyaWUxJafEiydNqKKpViKswBiylHD3ZJu2mrtQ/fhp7lgOPpMP72IQX4Ncwzdg==
dependencies: dependencies:
"@uniswap/permit2-sdk" "^1.2.0" "@uniswap/permit2-sdk" "^1.2.0"
"@uniswap/sdk-core" "^3.0.1" "@uniswap/sdk-core" "^3.0.1"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment