2020-05-13 15:29:54 +03:00
|
|
|
// ***********************************************************
|
|
|
|
// This file is processed and loaded automatically before your test files.
|
|
|
|
//
|
|
|
|
// You can read more here:
|
|
|
|
// https://on.cypress.io/configuration
|
|
|
|
// ***********************************************************
|
|
|
|
|
2023-03-27 19:48:02 +03:00
|
|
|
import '@cypress/code-coverage/support'
|
2023-04-28 18:46:42 +03:00
|
|
|
import 'cypress-hardhat/lib/browser'
|
2022-06-14 22:40:52 +03:00
|
|
|
|
2023-04-26 00:24:57 +03:00
|
|
|
import { Eip1193Bridge } from '@ethersproject/experimental/lib/eip1193-bridge'
|
2023-04-25 02:00:04 +03:00
|
|
|
import assert from 'assert'
|
|
|
|
|
2022-09-09 21:54:54 +03:00
|
|
|
import { FeatureFlag } from '../../src/featureFlags/flags/featureFlags'
|
2023-04-26 00:24:57 +03:00
|
|
|
import { UserState } from '../../src/state/user/reducer'
|
|
|
|
import { CONNECTED_WALLET_USER_STATE } from '../utils/user-state'
|
2023-04-25 02:00:04 +03:00
|
|
|
import { injected } from './ethereum'
|
2022-09-09 21:54:54 +03:00
|
|
|
|
2022-06-14 22:40:52 +03:00
|
|
|
declare global {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
|
|
namespace Cypress {
|
|
|
|
interface ApplicationWindow {
|
2023-04-26 00:24:57 +03:00
|
|
|
ethereum: Eip1193Bridge
|
2022-06-14 22:40:52 +03:00
|
|
|
}
|
|
|
|
interface VisitOptions {
|
|
|
|
serviceWorker?: true
|
2022-09-09 21:54:54 +03:00
|
|
|
featureFlags?: Array<FeatureFlag>
|
2023-04-26 00:24:57 +03:00
|
|
|
/**
|
|
|
|
* The mock ethereum provider to inject into the page.
|
|
|
|
* @default 'goerli'
|
|
|
|
*/
|
|
|
|
// TODO(INFRA-175): Migrate all usage of 'goerli' to 'hardhat'.
|
|
|
|
ethereum?: 'goerli' | 'hardhat'
|
|
|
|
/**
|
|
|
|
* Initial user state.
|
|
|
|
* @default {@type import('../utils/user-state').CONNECTED_WALLET_USER_STATE}
|
|
|
|
*/
|
|
|
|
userState?: Partial<UserState>
|
|
|
|
}
|
2022-06-14 22:40:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sets up the injected provider to be a mock ethereum provider with the given mnemonic/index
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
Cypress.Commands.overwrite(
|
|
|
|
'visit',
|
|
|
|
(original, url: string | Partial<Cypress.VisitOptions>, options?: Partial<Cypress.VisitOptions>) => {
|
|
|
|
assert(typeof url === 'string')
|
|
|
|
|
2023-04-26 00:24:57 +03:00
|
|
|
// Add a hash in the URL if it is not present (to use hash-based routing correctly with queryParams).
|
|
|
|
let hashUrl = url.startsWith('/') && url.length > 2 && !url.startsWith('/#') ? `/#${url}` : url
|
|
|
|
if (options?.ethereum === 'goerli') hashUrl += `${url.includes('?') ? '&' : '?'}chain=goerli`
|
|
|
|
|
|
|
|
return cy
|
|
|
|
.intercept('/service-worker.js', options?.serviceWorker ? undefined : { statusCode: 404 })
|
2023-04-28 18:46:42 +03:00
|
|
|
.provider()
|
|
|
|
.then((provider) =>
|
2023-04-26 00:24:57 +03:00
|
|
|
original({
|
|
|
|
...options,
|
|
|
|
url: hashUrl,
|
|
|
|
onBeforeLoad(win) {
|
|
|
|
options?.onBeforeLoad?.(win)
|
2022-12-20 23:36:57 +03:00
|
|
|
|
2023-04-26 00:24:57 +03:00
|
|
|
// We want to test from a clean state, so we clear the local storage (which clears redux).
|
|
|
|
win.localStorage.clear()
|
2022-09-09 21:54:54 +03:00
|
|
|
|
2023-04-26 00:24:57 +03:00
|
|
|
// Set initial user state.
|
|
|
|
win.localStorage.setItem(
|
|
|
|
'redux_localstorage_simple_user', // storage key for the user reducer using 'redux-localstorage-simple'
|
|
|
|
JSON.stringify(options?.userState ?? CONNECTED_WALLET_USER_STATE)
|
2022-09-09 21:54:54 +03:00
|
|
|
)
|
|
|
|
|
2023-04-26 00:24:57 +03:00
|
|
|
// Set feature flags, if configured.
|
|
|
|
if (options?.featureFlags) {
|
|
|
|
const featureFlags = options.featureFlags.reduce((flags, flag) => ({ ...flags, [flag]: 'enabled' }), {})
|
|
|
|
win.localStorage.setItem('featureFlags', JSON.stringify(featureFlags))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject the mock ethereum provider.
|
|
|
|
if (options?.ethereum === 'hardhat') {
|
2023-04-28 18:46:42 +03:00
|
|
|
win.ethereum = provider
|
2023-04-26 00:24:57 +03:00
|
|
|
} else {
|
|
|
|
win.ethereum = injected
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
2022-06-14 22:40:52 +03:00
|
|
|
}
|
|
|
|
)
|
2022-06-14 00:43:58 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-05-04 18:31:53 +03:00
|
|
|
cy
|
|
|
|
// Many API calls enforce that requests come from our app, so we must mock Origin and Referer.
|
|
|
|
.intercept('*', (req) => {
|
|
|
|
req.headers['referer'] = 'https://app.uniswap.org'
|
|
|
|
req.headers['origin'] = 'https://app.uniswap.org'
|
|
|
|
})
|
2023-04-29 01:29:16 +03:00
|
|
|
// Infura uses a test endpoint, which allow-lists http://localhost:3000 instead.
|
|
|
|
.intercept(/infura.io/, (req) => {
|
|
|
|
req.headers['referer'] = 'http://localhost:3000'
|
|
|
|
req.headers['origin'] = 'http://localhost:3000'
|
|
|
|
req.alias = req.body.method
|
|
|
|
req.continue()
|
|
|
|
})
|
|
|
|
// Mock Amplitude responses to avoid analytics from tests.
|
|
|
|
.intercept('https://api.uniswap.org/v1/amplitude-proxy', (req) => {
|
|
|
|
const requestBody = JSON.stringify(req.body)
|
|
|
|
const byteSize = new Blob([requestBody]).size
|
|
|
|
req.reply(
|
|
|
|
JSON.stringify({
|
|
|
|
code: 200,
|
|
|
|
server_upload_time: Date.now(),
|
|
|
|
payload_size_bytes: byteSize,
|
|
|
|
events_ingested: req.body.events.length,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
2023-05-04 18:31:53 +03:00
|
|
|
// Reset hardhat between tests to ensure isolation.
|
|
|
|
// This resets the fork, as well as options like automine.
|
|
|
|
.hardhat()
|
|
|
|
.then((hardhat) => hardhat.reset())
|
2022-06-14 00:43:58 +03:00
|
|
|
})
|
2022-07-18 16:46:31 +03:00
|
|
|
|
2022-12-21 02:08:20 +03:00
|
|
|
Cypress.on('uncaught:exception', () => {
|
|
|
|
// returning false here prevents Cypress from failing the test
|
2022-07-18 16:46:31 +03:00
|
|
|
return false
|
|
|
|
})
|