How to test the value of an argument using Mocha and assert.throws in JavaScript? -
i want see if value equals 'x' cause function throw exception. have unit test checks if block throw exception, want exception thrown if argument = 'x', not if empty. how do using assert?
here's code illustrate mean:
// function check if color not transparent. function checkcolor(color) { if (color == 'transparent') { throw new typeerror('cant have transparent colors!'); } else { return color; } }
here's assert:
assert.throws(checkcolor, /cant have transparent colors!/);
now, know assertion fail because function throws exception if color == 'transparent'. using mocha , assert, how test assertion want it? don't want merely test if color !== undefined or of type. want see if exception raised under specific circumstance above - circumstance want exception raised (since having empty color, handled elsewhere).
assert.throw(function() { ithrowerror(argument) }, error)
now make sure argument = x;
Comments
Post a Comment