From 67581ce4820b5c5bc909dd26acabc7356dbd58b4 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 18 Feb 2020 15:21:25 -0500 Subject: [PATCH] feat(@embark/test-runner): make evmMethod globally available + docs --- packages/stack/test-runner/src/lib/index.js | 5 +++++ site/source/docs/contracts_testing.md | 22 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/stack/test-runner/src/lib/index.js b/packages/stack/test-runner/src/lib/index.js index 7dbd410ed6..b4b05536e9 100644 --- a/packages/stack/test-runner/src/lib/index.js +++ b/packages/stack/test-runner/src/lib/index.js @@ -177,6 +177,8 @@ class TestRunner { } }; + global.evmMethod = this.evmMethod.bind(this); + global.getEvmVersion = async () => { return this.evmMethod('web3_clientVersion'); }; @@ -382,6 +384,9 @@ class TestRunner { if (error) { return reject(error); } + if (res.error) { + return reject(new Error(res.error)); + } resolve(res.result); } ); diff --git a/site/source/docs/contracts_testing.md b/site/source/docs/contracts_testing.md index f019905bd0..5c83eaf564 100644 --- a/site/source/docs/contracts_testing.md +++ b/site/source/docs/contracts_testing.md @@ -293,7 +293,7 @@ This function lets you increase the time of the EVM. It is useful in the case wh await increaseTime(amount); ``` -`amount`: [Number] Number of seconds to increase +- `amount`: [Number] Number of seconds to increase ```javascript it("should have expired after increasing time", async function () { @@ -325,6 +325,26 @@ await getEvmVersion(); Returns a string, eg: `EthereumJS TestRPC/v2.9.2/ethereum-js` +### evmMethod + +If there are EVM methods that are not supported by the web3 library you use, Embark exposes the global function `evmMethod` that lets you call the RPC method directly. + +#### Syntax +`evmMethod(rpcMethodName, parameters)` + + - `rpcMethodName`: [string] Name of the RPC method to call. + - `parameters`: [Array] Optional array of parameters, as specified by the RPC method API. + +#### Usage +For example, let's say you are using `web3.js` in your tests, but would like to call the `eth_signTypedData` RPC method. Because `web3.js` does not support this method, it won't be possible to use `web3.js` for this call. Instead, we can call the `eth_signTypedData` RPC method in our tests using the global `evmMethod` function: + +```javascript + const signature = await evmMethod("eth_signTypedData", [ + accounts[0], + data +]); +``` + ## Code coverage Embark allows you to generate a coverage report for your Solidity Smart Contracts by passing the `--coverage` option on the `embark test` command.