2023-05-09 20:33:55 +03:00
|
|
|
// @ts-ignore
|
|
|
|
import TokenListJSON from '@uniswap/default-token-list'
|
2023-06-07 19:29:23 +03:00
|
|
|
import { CyHttpMessages } from 'cypress/types/net-stubbing'
|
2023-05-09 20:33:55 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// Many API calls enforce that requests come from our app, so we must mock Origin and Referer.
|
|
|
|
cy.intercept('*', (req) => {
|
|
|
|
req.headers['referer'] = 'https://app.uniswap.org'
|
|
|
|
req.headers['origin'] = 'https://app.uniswap.org'
|
|
|
|
})
|
|
|
|
|
2023-09-22 22:05:27 +03:00
|
|
|
// Network RPCs are disabled for cypress tests - calls should be routed through the connected wallet instead.
|
2023-08-02 08:13:19 +03:00
|
|
|
cy.intercept(/infura.io/, { statusCode: 404 })
|
2023-09-22 22:05:27 +03:00
|
|
|
cy.intercept(/quiknode.pro/, { statusCode: 404 })
|
2023-05-09 20:33:55 +03:00
|
|
|
|
2023-06-07 19:29:23 +03:00
|
|
|
// Log requests to hardhat.
|
|
|
|
cy.intercept(/:8545/, logJsonRpc)
|
|
|
|
|
2023-05-09 20:33:55 +03:00
|
|
|
// Mock analytics responses to avoid analytics in tests.
|
|
|
|
cy.intercept('https://api.uniswap.org/v1/amplitude-proxy', (req) => {
|
|
|
|
const requestBody = JSON.stringify(req.body)
|
|
|
|
const byteSize = new Blob([requestBody]).size
|
2023-08-03 21:33:16 +03:00
|
|
|
req.alias = 'amplitude'
|
2023-05-09 20:33:55 +03:00
|
|
|
req.reply(
|
|
|
|
JSON.stringify({
|
|
|
|
code: 200,
|
|
|
|
server_upload_time: Date.now(),
|
|
|
|
payload_size_bytes: byteSize,
|
|
|
|
events_ingested: req.body.events.length,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}).intercept('https://*.sentry.io', { statusCode: 200 })
|
|
|
|
|
|
|
|
// Mock our own token list responses to avoid the latency of IPFS.
|
|
|
|
cy.intercept('https://gateway.ipfs.io/ipns/tokens.uniswap.org', TokenListJSON)
|
|
|
|
.intercept('https://gateway.ipfs.io/ipns/extendedtokens.uniswap.org', { statusCode: 404 })
|
|
|
|
.intercept('https://gateway.ipfs.io/ipns/unsupportedtokens.uniswap.org', { statusCode: 404 })
|
|
|
|
|
|
|
|
// Reset hardhat between tests to ensure isolation.
|
|
|
|
// This resets the fork, as well as options like automine.
|
|
|
|
cy.hardhat().then((hardhat) => hardhat.reset())
|
|
|
|
})
|
2023-06-07 19:29:23 +03:00
|
|
|
|
|
|
|
function logJsonRpc(req: CyHttpMessages.IncomingHttpRequest) {
|
|
|
|
req.alias = req.body.method
|
|
|
|
const log = Cypress.log({
|
|
|
|
autoEnd: false,
|
|
|
|
name: req.body.method,
|
2023-08-02 08:13:19 +03:00
|
|
|
message: req.body.params?.map((param: any) =>
|
2023-06-07 19:29:23 +03:00
|
|
|
typeof param === 'object' ? '{...}' : param?.toString().substring(0, 10)
|
|
|
|
),
|
|
|
|
})
|
|
|
|
req.on('after:response', (res) => {
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
log.end()
|
|
|
|
} else {
|
|
|
|
log.error(new Error(`${res.statusCode}: ${res.statusMessage}`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|