[Ethereum] Unit testing for assert()/require() in Truffle/Solidity

requiresoliditytruffle

If I write a function like so:

function calculate(int x) public returns (int) {
    require(x > 0);
    // Do other stuff
    return x;
}

How would I write a JavaScript unit test in Truffle verifying that the require fails when I try to call the function with a parameter of -1?

Obviously, this is just an illustrative example. I can't seem to find any documentation online.

it("should never be zero", function () {
    return ContractName.deployed().then(function(instance) {
        return instance.calculate.call(0);
    }).then(function (result) {
        // What do I put here?
    });
})

Best Answer

require('chai')
 .use(require('chai-as-promised'))
 .should();
const ERROR_MSG = 'VM Exception while processing transaction: revert';
ContractName = await ContractName.new();
it('should never be zero', async() => {
   await ContractName
   .calculate(-1)
   .should.be.rejectedWith(ERROR_MSG);
})