In the last exercise, you learned how to organize and automate tests using the Mocha test framework. To write the tests themselves, we can use the
assert.ok method provided by Node.js.
In programming, a test compares an expected outcome to an actual outcome. For example, we expect the outcome of the following code…
const a = 1 + 2;
a has a value of
3. To test the value saved to
a to the expected result. Inside the statement, you would construct an error when the actual outcome does not match the expected.
assert.ok() allows you to compare values and throw errors as needed using one function call. The small, human-readable format of the functions will help you make a more expressive test suite.
As a Node module,
assert can be imported at the top of your files with
const assert = require('assert');
assert functions like this:
assert.ok(a === 3);
In this case
a === 3 evaluates to
true, so no error is thrown.
If an argument passed to
assert.ok() evaluates to false, an
AssertionError is thrown. The error communicates to Mocha that a test has failed, and Mocha logs the error message to the console.
At the top of index_test.js, import
assert with the
Within the provided
it block, enter the following expression:
assert.ok(3 + 4 === 8);
Run the test suite.
The argument passed to
assert.ok() throws an error! You should see some descriptive feedback in the console.
Make the expression evaluate to true by replacing
Run the suite again. Your test should pass now!