Assertions
Assertions are used to check the behaviour of your code, you could manually check conditions and throw exceptions, but assertions give you short statements that are easier to read. For example you could write the following manual assertion:
ExamplesTest = (function() {
return {
addTwoNumbersWith2and3Expect5: function() {
var result = Examples.addTwoNumbers(2, 3);
if (5 !== result) {
throw 'The result wasn\'t 5';
}
}
};
})();
Enhance.discoverTests(ExamplesTest).runTests();
But it is much simpler to use an assertion like this:
ExamplesTest = (function() {
return {
addTwoNumbersWith2and3Expect5: function() {
var result = Examples.addTwoNumbers(2, 3);
Assert.areIdentical(5, result);
}
};
})();
Enhance.discoverTests(ExamplesTest).runTests();
And you'll also get a neat and predictable error message each time.
Assert.areIdentical(expected, actual);
This call verifies that the types and values are identical. For example:
Assert.areIdentical(1, 1); // passes
Assert.areIdentical(1, 2); // fails
Assert.areIdentical(1, '1'); // fails
Assert.areNotIdentical(expected, actual);
This call verifies that the types and/or values are not identical. For example:
Assert.areNotIdentical(1, 2); // passes
Assert.areNotIdentical(1, '1'); // passes
Assert.areNotIdentical(1, 1); // fails
Assert.isTrue(actual);
This call verifies that the type is boolean and that the value is true:
Assert.isTrue(true); // passes
Assert.isTrue(false); // fails
Assert.isTrue(1); // fails
Assert.isFalse(actual);
This call verifies that the type is boolean and that the value is false:
Assert.isFalse(false); // passes
Assert.isFalse(true); // fails
Assert.isFalse(0); // fails
Assert.isTruthy(actual);
This call verifies that the value is truthy, i.e. can be converted to true or has a value:
Assert.isTruthy(true); // passes
Assert.isTruthy(1); // passes
Assert.isTruthy(false); // fails
Assert.isTruthy(0); // fails
Assert.isFalsey(actual);
This call verifies that the type is falsey, i.e. cannot be converted to true or has no value
Assert.isFalsey(false); // passes
Assert.isFalsey(0); // passes
Assert.isFalsey(''); // passes
Assert.isFalsey(true); // fails
Assert.isFalsey(1); // fails
Assert.isNull(actual);
This call verifies that the parameter is null:
Assert.isFalse(null); // passes
Assert.isFalse(1); // fails
Assert.isFalse(''); // fails
Assert.contains(expected, actual);
This call verifies that the expected value is within the actual value:
Assert.contains('Hello', 'Hello World'); // passes
Assert.contains('Hi', 'Hello World'); // fails
Assert.notContains(expected, actual);
This call verifies that the expected value is not within the actual value:
Assert.notContains('Hi', 'Hello World'); // passes
Assert.notContains('Hello', 'Hello World'); // fails
Assert.doesThrow(method, args);
This call verifies that the function supplied (with optional arguments) throws an exception:
Assert.doesThrow(function () { throw 'Exception'; }); // passes
Assert.doesThrow(function (msg) { throw msg; }, ['some value']); // passes
Assert.notContains(function () { return; }); // fails
Assert.fails();
This call fails the test, use to fail a test based on your own conditions:
Assert.fails(); // fails
