I had the same issue as you mentioned in the comment whereby my test which tests no Error
is thrown would stop "badly" showing a badly formatted Died on test #1
message without any useful information.
I ended up using a mix of both; raises()
for one test and try/catch
for the other.
I used raises() for the test which tests that an Error
is thrown, similar to this:
test("When myFunction() is called with a invalid instance Then Error is thrown", function () {
// Arrange
var testInstance = {};
// Act
raises(function() {
myFunction(testInstance);
}, Error, "myFunction() should throw an Error");
// Assert
// raises() does assertion
});
If the above throws an Error
all is fine and if not a nice formatted message is displayed, similar to this:
myFunction() should throw Error
Result: No exception was thrown.
I then used try/catch
for the tests which have to ensure no Error
is thrown, similar to this:
test("When myFunction() is called with a valid instance Then no Error is thrown", function () {
// Arrange
var testInstance = new myObject();
var result;
// Act
try {
myFunction(testInstance);
result = true;
} catch(error) {
result = false;
}
// Assert
ok(result, "myFunction() should not throw an Error");
});
If the above throws no Error
all is fine and if an Error
is thrown a nice formatted message is displayed, similar to this:
myFunction() should not throw an Error
Source: ...