JavaScript for impatient programmers (beta)
Please support this book: buy it or donate
(Ad, please don’t block.)

8. Getting started with quizzes and exercises

At the end of most chapters, there are quizzes and exercises. These are a paid feature, but a comprehensive preview is available. This chapter explains how to get started with them.

8.1. Quizzes


Running the quiz app:

8.2. Exercises

8.2.1. Installing the exercises

To install the exercises:

8.2.2. Running exercises

8.3. Unit tests in JavaScript

All exercises in this book are tests that are run via the test framework Mocha. This section gives a brief introduction.

8.3.1. A typical test

Typical test code is split into two parts:

Take, for example, the following two files: Part 1: the code

The code itself resides in id.js:

export function id(x) {
  return x;

The key thing here is: everything you want to test must be exported. Otherwise, the test code can’t access it. Part 2: the tests

The tests for the code reside in id_test.js:

import {strict as assert} from 'assert'; // (A)
import {id} from './id.js'; // (B)

test('My test', () => { // (C)
  assert.equal(id('abc'), 'abc'); // (D)

You don’t need to worry too much about the syntax: You won’t have to write this kind of code yourself – all tests are written for you.

The core of this test file resides in line D – a so-called assertion: assert.equal() specifies that the expected result of id('abc') is 'abc'. The assertion library, a built-in Node.js module called assert, is documented in the next chapter.

As for the other lines:

To run the test, we execute the following in a command line:

npm t demos/syntax/id_test.js

The t is an abbreviation for test. That is, the long version of this command is:

npm text demos/syntax/id_test.js

  Exercise: Your first exercise

The following exercise gives you a first taste of what exercises are like: exercises/syntax/first_module_test.js

8.3.2. Asynchronous tests in mocha


You can postpone reading this section until you get to the chapters on asynchronous programming.

Writing tests for asynchronous code requires extra work: The test receives its results later and has to signal to mocha that it isn’t finished, yet, when it returns. The following subsections examine three ways of doing so. Calling done()

A test becomes asynchronous if it has at least one parameter. That parameter is usually called done and receives a function that you call once your code is finished:

test('addViaCallback', (done) => {
  addViaCallback(1, 2, (error, result) => {
    if (error) {
    } else {
      assert.strictEqual(result, 3);
}); Returning a Promise

A test also becomes asynchronous if it returns a Promise. Mocha considers the test to be finished once the Promise is either fulfilled or rejected. A test is considered successful if the Promise is fulfilled and failed if the Promise is rejected.

test('addAsync', () => {
  return addAsync(1, 2)
  .then(result => {
    assert.strictEqual(result, 3);
}); The test is an async function

Async functions always return Promises. Therefore, an async function is a convenient way of implementing an asynchronous test. The following code is equivalent to the previous example.

test('addAsync', async () => {
  const result = await addAsync(1, 2);
  assert.strictEqual(result, 3);
  // No explicit return necessary!

You don’t need to explicitly return anything: The implicitly returned undefined is used to fulfill the Promise returned by this async function. And if the test code throws an exception then the async function takes care of rejecting the returned Promise.