uniswap-interface-uncensored/cypress/support/e2e.ts

128 lines
4.5 KiB
TypeScript
Raw Normal View History

// ***********************************************************
// This file is processed and loaded automatically before your test files.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************
import '@cypress/code-coverage/support'
2023-04-28 18:46:42 +03:00
import 'cypress-hardhat/lib/browser'
feat: service worker with etag cache (#3897) * fix: always-fresh service worker cache * chore: clarify service-worker * fix: cache in CacheStorage * feat: set __isDocumentCached * add back in manifest precaching * add unit tests (incomplete) * test: simplify test env * test: add service-worker cypress test * test: service-worker document handler * fix: CachedDocument ctor * fix: Readable for ReadableStream in jest * build: clean up module loading * fix: rename commands->ethereum * build: simplify package.json deps * build: clean up cypress usage * build: clean up yarn.lock * build: record cypress runs * build: disable chromeWebSecurity in cypress tests * build: rm babel * build: disable sw in ci cypress * build: nits * build: update workbox version * chore: fix merge * test: cache * test: cypress-ify the before hook * test: clear sw before each test * fix: cy then * test: cypress shenanigans * style: lint * chore: rm todo * test: fail fast for service worker with dev builds * docs: update contributing to tests * fix: clean up tests after merge - Add fast fail in case of dev server, which lacks ServiceWorker * fix: inject ethereum * test: service worker * test: increase sw timeout * test: sw state * test: run cypress in chrome * feat: add on-demand caching to improve sw startup time * test: test dynamically * fix: simplify cached doc * fix: optional sw * fix: expose response on cached doc * fix: stub out sw req * fix: intercept Co-authored-by: Christine Legge <christine.legge@uniswap.org>
2022-06-14 22:40:52 +03:00
import { Eip1193Bridge } from '@ethersproject/experimental/lib/eip1193-bridge'
import assert from 'assert'
import { FeatureFlag } from '../../src/featureFlags/flags/featureFlags'
import { UserState } from '../../src/state/user/reducer'
import { CONNECTED_WALLET_USER_STATE } from '../utils/user-state'
import { injected } from './ethereum'
feat: service worker with etag cache (#3897) * fix: always-fresh service worker cache * chore: clarify service-worker * fix: cache in CacheStorage * feat: set __isDocumentCached * add back in manifest precaching * add unit tests (incomplete) * test: simplify test env * test: add service-worker cypress test * test: service-worker document handler * fix: CachedDocument ctor * fix: Readable for ReadableStream in jest * build: clean up module loading * fix: rename commands->ethereum * build: simplify package.json deps * build: clean up cypress usage * build: clean up yarn.lock * build: record cypress runs * build: disable chromeWebSecurity in cypress tests * build: rm babel * build: disable sw in ci cypress * build: nits * build: update workbox version * chore: fix merge * test: cache * test: cypress-ify the before hook * test: clear sw before each test * fix: cy then * test: cypress shenanigans * style: lint * chore: rm todo * test: fail fast for service worker with dev builds * docs: update contributing to tests * fix: clean up tests after merge - Add fast fail in case of dev server, which lacks ServiceWorker * fix: inject ethereum * test: service worker * test: increase sw timeout * test: sw state * test: run cypress in chrome * feat: add on-demand caching to improve sw startup time * test: test dynamically * fix: simplify cached doc * fix: optional sw * fix: expose response on cached doc * fix: stub out sw req * fix: intercept Co-authored-by: Christine Legge <christine.legge@uniswap.org>
2022-06-14 22:40:52 +03:00
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cypress {
interface ApplicationWindow {
ethereum: Eip1193Bridge
feat: service worker with etag cache (#3897) * fix: always-fresh service worker cache * chore: clarify service-worker * fix: cache in CacheStorage * feat: set __isDocumentCached * add back in manifest precaching * add unit tests (incomplete) * test: simplify test env * test: add service-worker cypress test * test: service-worker document handler * fix: CachedDocument ctor * fix: Readable for ReadableStream in jest * build: clean up module loading * fix: rename commands->ethereum * build: simplify package.json deps * build: clean up cypress usage * build: clean up yarn.lock * build: record cypress runs * build: disable chromeWebSecurity in cypress tests * build: rm babel * build: disable sw in ci cypress * build: nits * build: update workbox version * chore: fix merge * test: cache * test: cypress-ify the before hook * test: clear sw before each test * fix: cy then * test: cypress shenanigans * style: lint * chore: rm todo * test: fail fast for service worker with dev builds * docs: update contributing to tests * fix: clean up tests after merge - Add fast fail in case of dev server, which lacks ServiceWorker * fix: inject ethereum * test: service worker * test: increase sw timeout * test: sw state * test: run cypress in chrome * feat: add on-demand caching to improve sw startup time * test: test dynamically * fix: simplify cached doc * fix: optional sw * fix: expose response on cached doc * fix: stub out sw req * fix: intercept Co-authored-by: Christine Legge <christine.legge@uniswap.org>
2022-06-14 22:40:52 +03:00
}
interface VisitOptions {
serviceWorker?: true
featureFlags?: Array<FeatureFlag>
/**
* 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>
}
feat: service worker with etag cache (#3897) * fix: always-fresh service worker cache * chore: clarify service-worker * fix: cache in CacheStorage * feat: set __isDocumentCached * add back in manifest precaching * add unit tests (incomplete) * test: simplify test env * test: add service-worker cypress test * test: service-worker document handler * fix: CachedDocument ctor * fix: Readable for ReadableStream in jest * build: clean up module loading * fix: rename commands->ethereum * build: simplify package.json deps * build: clean up cypress usage * build: clean up yarn.lock * build: record cypress runs * build: disable chromeWebSecurity in cypress tests * build: rm babel * build: disable sw in ci cypress * build: nits * build: update workbox version * chore: fix merge * test: cache * test: cypress-ify the before hook * test: clear sw before each test * fix: cy then * test: cypress shenanigans * style: lint * chore: rm todo * test: fail fast for service worker with dev builds * docs: update contributing to tests * fix: clean up tests after merge - Add fast fail in case of dev server, which lacks ServiceWorker * fix: inject ethereum * test: service worker * test: increase sw timeout * test: sw state * test: run cypress in chrome * feat: add on-demand caching to improve sw startup time * test: test dynamically * fix: simplify cached doc * fix: optional sw * fix: expose response on cached doc * fix: stub out sw req * fix: intercept Co-authored-by: Christine Legge <christine.legge@uniswap.org>
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')
// 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) =>
original({
...options,
url: hashUrl,
onBeforeLoad(win) {
options?.onBeforeLoad?.(win)
// We want to test from a clean state, so we clear the local storage (which clears redux).
win.localStorage.clear()
// 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)
)
// 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
} else {
win.ethereum = injected
}
},
})
)
feat: service worker with etag cache (#3897) * fix: always-fresh service worker cache * chore: clarify service-worker * fix: cache in CacheStorage * feat: set __isDocumentCached * add back in manifest precaching * add unit tests (incomplete) * test: simplify test env * test: add service-worker cypress test * test: service-worker document handler * fix: CachedDocument ctor * fix: Readable for ReadableStream in jest * build: clean up module loading * fix: rename commands->ethereum * build: simplify package.json deps * build: clean up cypress usage * build: clean up yarn.lock * build: record cypress runs * build: disable chromeWebSecurity in cypress tests * build: rm babel * build: disable sw in ci cypress * build: nits * build: update workbox version * chore: fix merge * test: cache * test: cypress-ify the before hook * test: clear sw before each test * fix: cy then * test: cypress shenanigans * style: lint * chore: rm todo * test: fail fast for service worker with dev builds * docs: update contributing to tests * fix: clean up tests after merge - Add fast fail in case of dev server, which lacks ServiceWorker * fix: inject ethereum * test: service worker * test: increase sw timeout * test: sw state * test: run cypress in chrome * feat: add on-demand caching to improve sw startup time * test: test dynamically * fix: simplify cached doc * fix: optional sw * fix: expose response on cached doc * fix: stub out sw req * fix: intercept Co-authored-by: Christine Legge <christine.legge@uniswap.org>
2022-06-14 22:40:52 +03:00
}
)
beforeEach(() => {
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'
})
// 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,
})
)
})
// 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', () => {
// returning false here prevents Cypress from failing the test
return false
})