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.
This commit is contained in:
Pascal Precht 2020-01-06 15:14:10 +01:00 committed by Iuri Matias
parent 1e1172e86c
commit e37d3f73ff
3 changed files with 22 additions and 1 deletions

View File

@ -19,6 +19,12 @@ contract("Expiration", function() {
}); });
it("should have expired after skipping time", async 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 await mineAtTimestamp(now + 1001); // sets block.timestamp to 1001
const isExpired = await Expiration.methods.isExpired().call(); const isExpired = await Expiration.methods.isExpired().call();
assert.strictEqual(isExpired, true); assert.strictEqual(isExpired, true);

View File

@ -155,6 +155,7 @@ class MochaTestRunner {
const provider = await this.events.request2("tests:blockchain:start", this.options); const provider = await this.events.request2("tests:blockchain:start", this.options);
this.web3 = new Web3(provider); this.web3 = new Web3(provider);
accounts = await this.web3.eth.getAccounts(); accounts = await this.web3.eth.getAccounts();
await events.request2("contracts:reset"); await events.request2("contracts:reset");
let contractFiles = await events.request2("config:contractsFiles"); let contractFiles = await events.request2("config:contractsFiles");

View File

@ -17,6 +17,7 @@ const reports = require('istanbul-reports');
const Reporter = require('./reporter'); const Reporter = require('./reporter');
const EMBARK_OPTION = 'embark'; const EMBARK_OPTION = 'embark';
const GANACHE_CLIENT_VERSION_NAME = "EthereumJS TestRPC";
class TestRunner { class TestRunner {
constructor(embark, options) { constructor(embark, options) {
@ -137,6 +138,7 @@ class TestRunner {
} }
setupGlobalVariables() { setupGlobalVariables() {
assert.reverts = async function(method, params = {}, message) { assert.reverts = async function(method, params = {}, message) {
if (typeof params === 'string') { if (typeof params === 'string') {
message = params; 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.embark = this.embark;
global.increaseTime = async (amount) => { 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_increaseTime", [Number(amount)]);
await this.evmMethod("evm_mine"); await this.evmMethod("evm_mine");
}; };
@ -187,6 +197,10 @@ class TestRunner {
// Mines a block and sets block.timestamp accordingly. // Mines a block and sets block.timestamp accordingly.
// See https://github.com/trufflesuite/ganache-core/pull/13 for more information // See https://github.com/trufflesuite/ganache-core/pull/13 for more information
global.mineAtTimestamp = async (timestamp) => { 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)]); return this.evmMethod("evm_mine", [parseFloat(timestamp)]);
}; };
} }