Skip to content

Commit

Permalink
feat(@embark/test-runner): expose evmClientVersion for conditional tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
0x-r4bbit authored and iurimatias committed Jan 9, 2020
1 parent 1e1172e commit e37d3f7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions dapps/tests/app/test/expiration_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/mocha-tests/src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
16 changes: 15 additions & 1 deletion packages/stack/test-runner/src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -137,6 +138,7 @@ class TestRunner {
}

setupGlobalVariables() {

assert.reverts = async function(method, params = {}, message) {
if (typeof params === 'string') {
message = params;
Expand Down Expand Up @@ -175,18 +177,30 @@ 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");
};

// 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)]);
};
}
Expand Down

0 comments on commit e37d3f7

Please sign in to comment.