Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
When TestRPC and Geth throw, they behave in a different manner. This Gist is an example on how to cover such a situation.
"use strict";
/**
* @param {!Function.<!Promise>} action.
* @param {!Number | !string | !BigNumber} gasToUse.
* @returns {!Promise} which throws unless it hit a valid error.
*/
module.exports = function expectedExceptionPromise(action, gasToUse) {
return new Promise(function (resolve, reject) {
try {
resolve(action());
} catch(e) {
reject(e);
}
})
.then(function (txObj) {
return typeof txObj === "string"
? web3.eth.getTransactionReceiptMined(txObj) // regular tx hash
: typeof txObj.receipt !== "undefined"
? txObj.receipt // truffle-contract function call
: typeof txObj.transactionHash === "string"
? web3.eth.getTransactionReceiptMined(txObj.transactionHash) // deployment
: txObj; // Unknown last case
})
.then(
function (receipt) {
// We are in Geth
if (typeof receipt.status !== "undefined") {
// Byzantium
assert.strictEqual(parseInt(receipt.status), 0, "should have reverted");
} else {
// Pre Byzantium
assert.equal(receipt.gasUsed, gasToUse, "should have used all the gas");
}
},
function (e) {
if ((e + "").indexOf("invalid JUMP") > -1 ||
(e + "").indexOf("out of gas") > -1 ||
(e + "").indexOf("invalid opcode") > -1 ||
(e + "").indexOf("revert") > -1) {
// We are in TestRPC
} else if ((e + "").indexOf("please check your gas amount") > -1) {
// We are in Geth for a deployment
} else {
throw e;
}
}
);
};
@xavierlepretre

This comment has been minimized.

Copy link
Owner Author

commented Oct 5, 2016

You can use it in this way:

const expectedExceptionPromise = require("expected_exception_testRPC_and_geth.js");

it("should not deploy my contract with value", function() {

    return expectedExceptionPromise(function () {
            return MyContract.new(
                "param1",
                2,
                { from: accounts[0], value: 1, gas: 3000000 });
        }, 3000000);

});
@xavierlepretre

This comment has been minimized.

Copy link
Owner Author

commented Oct 7, 2016

Or in a .then() promise:

it("should not be possible to do something invalid", function () {

    return MyContract.new({ from: accounts[0] })
        .then(function (newMyContract) {
            return extensions.expectedExceptionPromise(function () {
                    return newMyContract.myFunction("invalidParam1",
                        { from: accounts[0], gas: 3000000 });
                }, 3000000);
        })

});
@marekkirejczyk

This comment has been minimized.

Copy link

commented Apr 11, 2017

Great script, thanks!

One question:
Why don't you rethrow exception in lines 19 and 21?

@foogunlana

This comment has been minimized.

Copy link

commented Aug 27, 2017

@xavierlepretre amazing, thanks. I think there's a bug.

Line 18 might always evaluate to true.
Boolean("anything-bla-bla-bla".indexOf('invalid JUMP')) == true

@rowandh

This comment has been minimized.

Copy link

commented Sep 9, 2017

I believe this will successfully cause tests to pass in the cases where an exception occurs, however it will not cause tests to fail in the event that an exception does not occur, which is different from the behaviour I expected.

This can be rectified on line 23 by replacing throw e with assert.isTrue(false) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.