test:e2e - rework fetch-mocks

feature/default_network_editable
kumavis 5 years ago
parent 581763d510
commit 43007555bd
  1. 6
      test/e2e/fetch-mocks.js
  2. 5930
      test/e2e/fetch-mocks.json
  3. 23
      test/e2e/helpers.js
  4. 2
      test/e2e/metamask-ui.spec.js

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

@ -13,7 +13,7 @@ const {
installWebExt, installWebExt,
} = require('./func') } = require('./func')
const { until } = require('selenium-webdriver') const { until } = require('selenium-webdriver')
const fetchMockResponses = require('./fetch-mocks.js') const fetchMockResponses = require('./fetch-mocks.json')
const tinyDelayMs = 200 const tinyDelayMs = 200
const regularDelayMs = tinyDelayMs * 2 const regularDelayMs = tinyDelayMs * 2
@ -81,31 +81,36 @@ async function prepareExtensionForTesting () {
async function setupFetchMocking (driver) { async function setupFetchMocking (driver) {
// define fetchMocking script, to be evaluated in the browser // define fetchMocking script, to be evaluated in the browser
function fetchMocking () { function fetchMocking (fetchMockResponses) {
window.origFetch = window.fetch.bind(window) window.origFetch = window.fetch.bind(window)
window.fetch = async (...args) => { window.fetch = async (...args) => {
const url = args[0] const url = args[0]
if (url === 'https://ethgasstation.info/json/ethgasAPI.json') { if (url === 'https://ethgasstation.info/json/ethgasAPI.json') {
return { json: async () => JSON.parse(fetchMockResponses.ethGasBasic) } return { json: async () => clone(fetchMockResponses.ethGasBasic) }
} else if (url === 'https://ethgasstation.info/json/predictTable.json') { } else if (url === 'https://ethgasstation.info/json/predictTable.json') {
return { json: async () => JSON.parse(fetchMockResponses.ethGasPredictTable) } return { json: async () => clone(fetchMockResponses.ethGasPredictTable) }
} else if (url.match(/chromeextensionmm/)) { } else if (url.match(/chromeextensionmm/)) {
return { json: async () => JSON.parse(fetchMockResponses.metametrics) } return { json: async () => clone(fetchMockResponses.metametrics) }
} else if (url === 'https://dev.blockscale.net/api/gasexpress.json') { } else if (url === 'https://dev.blockscale.net/api/gasexpress.json') {
return { json: async () => JSON.parse(fetchMockResponses.gasExpress) } return { json: async () => clone(fetchMockResponses.gasExpress) }
} }
return window.origFetch(...args) return window.origFetch(...args)
} }
if (window.chrome && window.chrome.webRequest) {
window.chrome.webRequest.onBeforeRequest.addListener(cancelInfuraRequest, {urls: ['https://*.infura.io/*']}, ['blocking'])
}
function cancelInfuraRequest (requestDetails) { function cancelInfuraRequest (requestDetails) {
console.log(`fetchMocking - Canceling request: "${requestDetails.url}"`) console.log(`fetchMocking - Canceling request: "${requestDetails.url}"`)
return { cancel: true } return { cancel: true }
} }
if (window.chrome && window.chrome.webRequest) { function clone (obj) {
window.chrome.webRequest.onBeforeRequest.addListener(cancelInfuraRequest, {urls: ['https://*.infura.io/*']}, ['blocking']) return JSON.parse(JSON.stringify(obj))
} }
} }
// fetchMockResponses are parsed last minute to ensure that objects are uniquely instantiated
const fetchMockResponsesJson = JSON.stringify(fetchMockResponses)
// eval the fetchMocking script in the browser // eval the fetchMocking script in the browser
await driver.executeScript(`(${fetchMocking})()`) await driver.executeScript(`(${fetchMocking})(${fetchMockResponsesJson})`)
} }
async function loadExtension (driver, extensionId) { async function loadExtension (driver, extensionId) {

@ -30,7 +30,7 @@ describe('MetaMask', function () {
const largeDelayMs = regularDelayMs * 2 const largeDelayMs = regularDelayMs * 2
this.timeout(0) this.timeout(0)
this.bail(true) // this.bail(true)
before(async function () { before(async function () {
const result = await prepareExtensionForTesting() const result = await prepareExtensionForTesting()

Loading…
Cancel
Save