From e37d3f73ff6e8f1cde8c03312881bf1fc36492d8 Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Mon, 6 Jan 2020 15:14:10 +0100 Subject: [PATCH] feat(@embark/test-runner): expose evmClientVersion for conditional tests This commit introduces a new `global.getEvmVersion()` that can be used to conditionally run tests, such as when tests rely on RPC APIs that are only available in specific evm nodes. --- dapps/tests/app/test/expiration_spec.js | 6 ++++++ packages/plugins/mocha-tests/src/lib/index.js | 1 + packages/stack/test-runner/src/lib/index.js | 16 +++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/dapps/tests/app/test/expiration_spec.js b/dapps/tests/app/test/expiration_spec.js index de9f54a64a..f09170832c 100644 --- a/dapps/tests/app/test/expiration_spec.js +++ b/dapps/tests/app/test/expiration_spec.js @@ -19,6 +19,12 @@ contract("Expiration", function() { }); it("should have expired after skipping time", async function () { + const client = await getEvmVersion(); + + if (client.indexOf('EthereumJS TestRPC') === -1) { + console.info(`Skipping test because it requires the use of Ganache. Current blockchain client: ${client}`); + return assert.ok(true); + } await mineAtTimestamp(now + 1001); // sets block.timestamp to 1001 const isExpired = await Expiration.methods.isExpired().call(); assert.strictEqual(isExpired, true); diff --git a/packages/plugins/mocha-tests/src/lib/index.js b/packages/plugins/mocha-tests/src/lib/index.js index a798b42330..d3581bebf4 100644 --- a/packages/plugins/mocha-tests/src/lib/index.js +++ b/packages/plugins/mocha-tests/src/lib/index.js @@ -155,6 +155,7 @@ class MochaTestRunner { const provider = await this.events.request2("tests:blockchain:start", this.options); this.web3 = new Web3(provider); accounts = await this.web3.eth.getAccounts(); + await events.request2("contracts:reset"); let contractFiles = await events.request2("config:contractsFiles"); diff --git a/packages/stack/test-runner/src/lib/index.js b/packages/stack/test-runner/src/lib/index.js index 2323237c98..62295cad4e 100644 --- a/packages/stack/test-runner/src/lib/index.js +++ b/packages/stack/test-runner/src/lib/index.js @@ -17,6 +17,7 @@ const reports = require('istanbul-reports'); const Reporter = require('./reporter'); const EMBARK_OPTION = 'embark'; +const GANACHE_CLIENT_VERSION_NAME = "EthereumJS TestRPC"; class TestRunner { constructor(embark, options) { @@ -137,6 +138,7 @@ class TestRunner { } setupGlobalVariables() { + assert.reverts = async function(method, params = {}, message) { if (typeof params === 'string') { message = params; @@ -175,11 +177,19 @@ class TestRunner { } }; - global.assert = assert; + global.getEvmVersion = async () => { + return this.evmMethod('web3_clientVersion'); + }; + global.assert = assert; global.embark = this.embark; global.increaseTime = async (amount) => { + const evmVersion = await global.getEvmVersion(); + + if (evmVersion.indexOf(GANACHE_CLIENT_VERSION_NAME) === -1) { + this.logger.warn('WARNING: global.increaseTime uses RPC APIs that are only provided by a simulator (Ganache) and might cause a timeout'); + } await this.evmMethod("evm_increaseTime", [Number(amount)]); await this.evmMethod("evm_mine"); }; @@ -187,6 +197,10 @@ class TestRunner { // Mines a block and sets block.timestamp accordingly. // See https://github.com/trufflesuite/ganache-core/pull/13 for more information global.mineAtTimestamp = async (timestamp) => { + const evmVersion = await global.getEvmVersion(); + if (evmVersion.indexOf(GANACHE_CLIENT_VERSION_NAME) === -1) { + this.logger.warn('WARNING: global.mineAtTimestamp uses RPC APIs that are only provided by a simulator (Ganache) and might cause a timeout'); + } return this.evmMethod("evm_mine", [parseFloat(timestamp)]); }; }