1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Use Ganache programmatically in e2e tests (#7664)

The `ganache.js` helper module uses `ganache-core` to start `ganache`
instead of `ganache-cli`, and allows all of the same customization.
Using `ganache` programmatically from our e2e tests is much faster, as
we don't have to wait that arbitrary 5 seconds before each test as we
wait for `ganache-cli` to start up.
This commit is contained in:
Mark Stacey 2019-12-09 10:40:43 -04:00 committed by GitHub
parent 49a525b9f8
commit 476e422714
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 164 additions and 44 deletions

View File

@ -12,8 +12,11 @@ const {
setupFetchMocking, setupFetchMocking,
prepareExtensionForTesting, prepareExtensionForTesting,
} = require('./helpers') } = require('./helpers')
const Ganache = require('./ganache')
const enLocaleMessages = require('../../app/_locales/en/messages.json') const enLocaleMessages = require('../../app/_locales/en/messages.json')
const ganacheServer = new Ganache()
describe('MetaMask', function () { describe('MetaMask', function () {
let driver let driver
@ -26,6 +29,14 @@ describe('MetaMask', function () {
this.bail(true) this.bail(true)
before(async function () { before(async function () {
await ganacheServer.start({
accounts: [
{
secretKey: '0x53CB0AB5226EEBF4D872113D98332C1555DC304443BEE1CF759D15798D3C55A9',
balance: 25000000000000000000,
},
],
})
const result = await prepareExtensionForTesting() const result = await prepareExtensionForTesting()
driver = result.driver driver = result.driver
await setupFetchMocking(driver) await setupFetchMocking(driver)
@ -46,6 +57,7 @@ describe('MetaMask', function () {
}) })
after(async function () { after(async function () {
await ganacheServer.quit()
await driver.quit() await driver.quit()
}) })

View File

@ -14,8 +14,11 @@ const {
setupFetchMocking, setupFetchMocking,
prepareExtensionForTesting, prepareExtensionForTesting,
} = require('./helpers') } = require('./helpers')
const Ganache = require('./ganache')
const enLocaleMessages = require('../../app/_locales/en/messages.json') const enLocaleMessages = require('../../app/_locales/en/messages.json')
const ganacheServer = new Ganache()
describe('MetaMask', function () { describe('MetaMask', function () {
let driver let driver
let publicAddress let publicAddress
@ -28,6 +31,14 @@ describe('MetaMask', function () {
this.bail(true) this.bail(true)
before(async function () { before(async function () {
await ganacheServer.start({
accounts: [
{
secretKey: '0x53CB0AB5226EEBF4D872113D98332C1555DC304443BEE1CF759D15798D3C55A9',
balance: 25000000000000000000,
},
],
})
const result = await prepareExtensionForTesting() const result = await prepareExtensionForTesting()
driver = result.driver driver = result.driver
await setupFetchMocking(driver) await setupFetchMocking(driver)
@ -48,6 +59,7 @@ describe('MetaMask', function () {
}) })
after(async function () { after(async function () {
await ganacheServer.quit()
await driver.quit() await driver.quit()
}) })

View File

@ -12,8 +12,11 @@ const {
setupFetchMocking, setupFetchMocking,
prepareExtensionForTesting, prepareExtensionForTesting,
} = require('./helpers') } = require('./helpers')
const Ganache = require('./ganache')
const enLocaleMessages = require('../../app/_locales/en/messages.json') const enLocaleMessages = require('../../app/_locales/en/messages.json')
const ganacheServer = new Ganache()
describe('Using MetaMask with an existing account', function () { describe('Using MetaMask with an existing account', function () {
let driver let driver
@ -29,6 +32,14 @@ describe('Using MetaMask with an existing account', function () {
this.bail(true) this.bail(true)
before(async function () { before(async function () {
await ganacheServer.start({
accounts: [
{
secretKey: '0x53CB0AB5226EEBF4D872113D98332C1555DC304443BEE1CF759D15798D3C55A9',
balance: 25000000000000000000,
},
],
})
const result = await prepareExtensionForTesting() const result = await prepareExtensionForTesting()
driver = result.driver driver = result.driver
await setupFetchMocking(driver) await setupFetchMocking(driver)
@ -49,6 +60,7 @@ describe('Using MetaMask with an existing account', function () {
}) })
after(async function () { after(async function () {
await ganacheServer.quit()
await driver.quit() await driver.quit()
}) })

37
test/e2e/ganache.js Normal file
View File

@ -0,0 +1,37 @@
const ganache = require('ganache-core')
const { promisify } = require('util')
const defaultOptions = {
blockTime: 2,
network_id: 5777,
mnemonic: 'phrase upgrade clock rough situate wedding elder clever doctor stamp excess tent',
port: 8545,
vmErrorsOnRPCResponse: false,
}
class Ganache {
async start (options) {
options = Object.assign({}, defaultOptions, options)
const port = options.port
this._server = ganache.server(options)
const listen = promisify(this._server.listen).bind(this._server)
const blockchain = await listen(port)
return {
...blockchain,
port,
}
}
async quit () {
if (!this._server) {
throw new Error('Server not running yet')
}
const close = promisify(this._server.close).bind(this._server)
await close()
}
}
module.exports = Ganache

View File

@ -14,8 +14,11 @@ const {
setupFetchMocking, setupFetchMocking,
prepareExtensionForTesting, prepareExtensionForTesting,
} = require('./helpers') } = require('./helpers')
const Ganache = require('./ganache')
const enLocaleMessages = require('../../app/_locales/en/messages.json') const enLocaleMessages = require('../../app/_locales/en/messages.json')
const ganacheServer = new Ganache()
describe('MetaMask', function () { describe('MetaMask', function () {
let driver let driver
let publicAddress let publicAddress
@ -28,6 +31,18 @@ describe('MetaMask', function () {
this.bail(true) this.bail(true)
before(async function () { before(async function () {
await ganacheServer.start({
accounts: [
{
secretKey: '0x250F458997A364988956409A164BA4E16F0F99F916ACDD73ADCD3A1DE30CF8D1',
balance: 0,
},
{
secretKey: '0x53CB0AB5226EEBF4D872113D98332C1555DC304443BEE1CF759D15798D3C55A9',
balance: 25000000000000000000,
},
],
})
const result = await prepareExtensionForTesting() const result = await prepareExtensionForTesting()
driver = result.driver driver = result.driver
await setupFetchMocking(driver) await setupFetchMocking(driver)
@ -48,6 +63,7 @@ describe('MetaMask', function () {
}) })
after(async function () { after(async function () {
await ganacheServer.quit()
await driver.quit() await driver.quit()
}) })

View File

@ -12,8 +12,11 @@ const {
setupFetchMocking, setupFetchMocking,
prepareExtensionForTesting, prepareExtensionForTesting,
} = require('./helpers') } = require('./helpers')
const Ganache = require('./ganache')
const enLocaleMessages = require('../../app/_locales/en/messages.json') const enLocaleMessages = require('../../app/_locales/en/messages.json')
const ganacheServer = new Ganache()
describe('MetaMask', function () { describe('MetaMask', function () {
let driver let driver
@ -26,6 +29,7 @@ describe('MetaMask', function () {
this.bail(true) this.bail(true)
before(async function () { before(async function () {
await ganacheServer.start()
const result = await prepareExtensionForTesting({ responsive: true }) const result = await prepareExtensionForTesting({ responsive: true })
driver = result.driver driver = result.driver
await setupFetchMocking(driver) await setupFetchMocking(driver)
@ -46,6 +50,7 @@ describe('MetaMask', function () {
}) })
after(async function () { after(async function () {
await ganacheServer.quit()
await driver.quit() await driver.quit()
}) })

View File

@ -17,8 +17,11 @@ const {
setupFetchMocking, setupFetchMocking,
prepareExtensionForTesting, prepareExtensionForTesting,
} = require('./helpers') } = require('./helpers')
const Ganache = require('./ganache')
const enLocaleMessages = require('../../app/_locales/en/messages.json') const enLocaleMessages = require('../../app/_locales/en/messages.json')
const ganacheServer = new Ganache()
describe('MetaMask', function () { describe('MetaMask', function () {
let driver let driver
let tokenAddress let tokenAddress
@ -32,6 +35,7 @@ describe('MetaMask', function () {
this.bail(true) this.bail(true)
before(async function () { before(async function () {
await ganacheServer.start()
const result = await prepareExtensionForTesting() const result = await prepareExtensionForTesting()
driver = result.driver driver = result.driver
await setupFetchMocking(driver) await setupFetchMocking(driver)
@ -52,6 +56,7 @@ describe('MetaMask', function () {
}) })
after(async function () { after(async function () {
await ganacheServer.quit()
await driver.quit() await driver.quit()
}) })

View File

@ -15,8 +15,11 @@ const {
setupFetchMocking, setupFetchMocking,
prepareExtensionForTesting, prepareExtensionForTesting,
} = require('./helpers') } = require('./helpers')
const Ganache = require('./ganache')
const enLocaleMessages = require('../../app/_locales/en/messages.json') const enLocaleMessages = require('../../app/_locales/en/messages.json')
const ganacheServer = new Ganache()
describe('MetaMask', function () { describe('MetaMask', function () {
let driver let driver
let publicAddress let publicAddress
@ -29,6 +32,14 @@ describe('MetaMask', function () {
this.bail(true) this.bail(true)
before(async function () { before(async function () {
await ganacheServer.start({
accounts: [
{
secretKey: '0x53CB0AB5226EEBF4D872113D98332C1555DC304443BEE1CF759D15798D3C55A9',
balance: 25000000000000000000,
},
],
})
const result = await prepareExtensionForTesting() const result = await prepareExtensionForTesting()
driver = result.driver driver = result.driver
await setupFetchMocking(driver) await setupFetchMocking(driver)
@ -49,6 +60,7 @@ describe('MetaMask', function () {
}) })
after(async function () { after(async function () {
await ganacheServer.quit()
await driver.quit() await driver.quit()
}) })

View File

@ -5,93 +5,73 @@ set -e
set -u set -u
set -o pipefail set -o pipefail
# Set the environment variable 'GANACHE_ARGS' to change any optional ganache flags
# By default, the flag `--quiet` is used. Setting 'GANACHE_ARGS' will override the default.
OPTIONAL_GANACHE_ARGS="${GANACHE_ARGS---quiet}"
BASE_GANACHE_ARGS="${OPTIONAL_GANACHE_ARGS} --blockTime 2"
export PATH="$PATH:./node_modules/.bin" export PATH="$PATH:./node_modules/.bin"
export GANACHE_ARGS="${BASE_GANACHE_ARGS}"
concurrently --kill-others \ concurrently --kill-others \
--names 'ganache,dapp,e2e' \ --names 'dapp,e2e' \
--prefix '[{time}][{name}]' \ --prefix '[{time}][{name}]' \
--success first \ --success first \
'yarn ganache:start' \
'yarn dapp' \ 'yarn dapp' \
'sleep 5 && mocha test/e2e/metamask-ui.spec' 'mocha test/e2e/metamask-ui.spec'
concurrently --kill-others \ concurrently --kill-others \
--names 'ganache,dapp,e2e' \ --names 'dapp,e2e' \
--prefix '[{time}][{name}]' \ --prefix '[{time}][{name}]' \
--success first \ --success first \
'yarn ganache:start' \
'yarn dapp' \ 'yarn dapp' \
'sleep 5 && mocha test/e2e/metamask-responsive-ui.spec' 'mocha test/e2e/metamask-responsive-ui.spec'
concurrently --kill-others \ concurrently --kill-others \
--names 'ganache,dapp,e2e' \ --names 'dapp,e2e' \
--prefix '[{time}][{name}]' \ --prefix '[{time}][{name}]' \
--success first \ --success first \
'yarn ganache:start' \
'yarn dapp' \ 'yarn dapp' \
'sleep 5 && mocha test/e2e/signature-request.spec' 'mocha test/e2e/signature-request.spec'
export GANACHE_ARGS="${BASE_GANACHE_ARGS} --deterministic --account=0x53CB0AB5226EEBF4D872113D98332C1555DC304443BEE1CF759D15798D3C55A9,25000000000000000000"
concurrently --kill-others \
--names 'ganache,e2e' \
--prefix '[{time}][{name}]' \
--success first \
'yarn ganache:start' \
'sleep 5 && mocha test/e2e/from-import-ui.spec'
concurrently --kill-others \ concurrently --kill-others \
--names 'ganache,e2e' \ --names 'e2e' \
--prefix '[{time}][{name}]' \ --prefix '[{time}][{name}]' \
--success first \ --success first \
'npm run ganache:start' \ 'mocha test/e2e/from-import-ui.spec'
'sleep 5 && mocha test/e2e/send-edit.spec'
concurrently --kill-others \ concurrently --kill-others \
--names 'ganache,dapp,e2e' \ --names 'e2e' \
--prefix '[{time}][{name}]' \
--success first \
'mocha test/e2e/send-edit.spec'
concurrently --kill-others \
--names 'dapp,e2e' \
--prefix '[{time}][{name}]' \ --prefix '[{time}][{name}]' \
--success first \ --success first \
'yarn ganache:start' \
'yarn dapp' \ 'yarn dapp' \
'sleep 5 && mocha test/e2e/ethereum-on.spec' 'mocha test/e2e/ethereum-on.spec'
concurrently --kill-others \ concurrently --kill-others \
--names 'ganache,dapp,e2e' \ --names 'dapp,e2e' \
--prefix '[{time}][{name}]' \ --prefix '[{time}][{name}]' \
--success first \ --success first \
'yarn ganache:start' \
'yarn dapp' \ 'yarn dapp' \
'sleep 5 && mocha test/e2e/permissions.spec' 'mocha test/e2e/permissions.spec'
export GANACHE_ARGS="${BASE_GANACHE_ARGS} --deterministic --account=0x250F458997A364988956409A164BA4E16F0F99F916ACDD73ADCD3A1DE30CF8D1,0 --account=0x53CB0AB5226EEBF4D872113D98332C1555DC304443BEE1CF759D15798D3C55A9,25000000000000000000"
concurrently --kill-others \ concurrently --kill-others \
--names 'ganache,sendwithprivatedapp,e2e' \ --names 'sendwithprivatedapp,e2e' \
--prefix '[{time}][{name}]' \ --prefix '[{time}][{name}]' \
--success first \ --success first \
'npm run ganache:start' \
'npm run sendwithprivatedapp' \ 'npm run sendwithprivatedapp' \
'sleep 5 && mocha test/e2e/incremental-security.spec' 'mocha test/e2e/incremental-security.spec'
export GANACHE_ARGS="${BASE_GANACHE_ARGS} --deterministic --account=0x53CB0AB5226EEBF4D872113D98332C1555DC304443BEE1CF759D15798D3C55A9,25000000000000000000"
concurrently --kill-others \ concurrently --kill-others \
--names 'ganache,dapp,e2e' \ --names 'dapp,e2e' \
--prefix '[{time}][{name}]' \ --prefix '[{time}][{name}]' \
--success first \ --success first \
'yarn ganache:start' \
'yarn dapp' \ 'yarn dapp' \
'sleep 5 && mocha test/e2e/address-book.spec' 'mocha test/e2e/address-book.spec'
export GANACHE_ARGS="${BASE_GANACHE_ARGS} --deterministic --account=0x53CB0AB5226EEBF4D872113D98332C1555DC304443BEE1CF759D15798D3C55A9,25000000000000000000"
concurrently --kill-others \ concurrently --kill-others \
--names 'ganache,dapp,e2e' \ --names '3box,dapp,e2e' \
--prefix '[{time}][{name}]' \ --prefix '[{time}][{name}]' \
--success first \ --success first \
'node test/e2e/mock-3box/server.js' \ 'node test/e2e/mock-3box/server.js' \
'yarn ganache:start' \
'yarn dapp' \ 'yarn dapp' \
'sleep 5 && mocha test/e2e/threebox.spec' 'mocha test/e2e/threebox.spec'

View File

@ -12,8 +12,11 @@ const {
setupFetchMocking, setupFetchMocking,
prepareExtensionForTesting, prepareExtensionForTesting,
} = require('./helpers') } = require('./helpers')
const Ganache = require('./ganache')
const enLocaleMessages = require('../../app/_locales/en/messages.json') const enLocaleMessages = require('../../app/_locales/en/messages.json')
const ganacheServer = new Ganache()
describe('Using MetaMask with an existing account', function () { describe('Using MetaMask with an existing account', function () {
let driver let driver
@ -26,6 +29,14 @@ describe('Using MetaMask with an existing account', function () {
this.bail(true) this.bail(true)
before(async function () { before(async function () {
await ganacheServer.start({
accounts: [
{
secretKey: '0x53CB0AB5226EEBF4D872113D98332C1555DC304443BEE1CF759D15798D3C55A9',
balance: 25000000000000000000,
},
],
})
const result = await prepareExtensionForTesting() const result = await prepareExtensionForTesting()
driver = result.driver driver = result.driver
await setupFetchMocking(driver) await setupFetchMocking(driver)
@ -46,6 +57,7 @@ describe('Using MetaMask with an existing account', function () {
}) })
after(async function () { after(async function () {
await ganacheServer.quit()
await driver.quit() await driver.quit()
}) })

View File

@ -15,8 +15,11 @@ const {
setupFetchMocking, setupFetchMocking,
prepareExtensionForTesting, prepareExtensionForTesting,
} = require('./helpers') } = require('./helpers')
const Ganache = require('./ganache')
const enLocaleMessages = require('../../app/_locales/en/messages.json') const enLocaleMessages = require('../../app/_locales/en/messages.json')
const ganacheServer = new Ganache()
describe('MetaMask', function () { describe('MetaMask', function () {
let driver let driver
let publicAddress let publicAddress
@ -29,6 +32,7 @@ describe('MetaMask', function () {
this.bail(true) this.bail(true)
before(async function () { before(async function () {
await ganacheServer.start()
const result = await prepareExtensionForTesting() const result = await prepareExtensionForTesting()
driver = result.driver driver = result.driver
await setupFetchMocking(driver) await setupFetchMocking(driver)
@ -49,6 +53,7 @@ describe('MetaMask', function () {
}) })
after(async function () { after(async function () {
await ganacheServer.quit()
await driver.quit() await driver.quit()
}) })

View File

@ -12,8 +12,11 @@ const {
setupFetchMocking, setupFetchMocking,
prepareExtensionForTesting, prepareExtensionForTesting,
} = require('./helpers') } = require('./helpers')
const Ganache = require('./ganache')
const enLocaleMessages = require('../../app/_locales/en/messages.json') const enLocaleMessages = require('../../app/_locales/en/messages.json')
const ganacheServer = new Ganache()
describe('MetaMask', function () { describe('MetaMask', function () {
let driver let driver
@ -26,6 +29,14 @@ describe('MetaMask', function () {
this.bail(true) this.bail(true)
before(async function () { before(async function () {
await ganacheServer.start({
accounts: [
{
secretKey: '0x53CB0AB5226EEBF4D872113D98332C1555DC304443BEE1CF759D15798D3C55A9',
balance: 25000000000000000000,
},
],
})
const result = await prepareExtensionForTesting() const result = await prepareExtensionForTesting()
driver = result.driver driver = result.driver
await setupFetchMocking(driver) await setupFetchMocking(driver)
@ -46,6 +57,7 @@ describe('MetaMask', function () {
}) })
after(async function () { after(async function () {
await ganacheServer.quit()
await driver.quit() await driver.quit()
}) })