NML Says

Testing JavaScript in Node

Return to main lesson

Here is a JavaScript example of a test script: The to programs you will see are quoted from https://blog.logrocket.com/node-js-express-test-driven-development-jest/, and then adapted to fit better into our style guide, and adapted a bit to include a couple of extra points.

Example 1. A Test Suite, logrocketjs/tests/index.test.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const test = require('node:test');			  // new in node as of V18
const assert = require('assert/strict');

const {										 // the functions to be tested
    calcAge,
    createBox,
    canDrive,
    powerLevel,
    workSchedule,
} = require('../index');

// Calculates how old someone is and depending on the year this test could pass or fail
test('calculates age', function() {
    return assert.equal(calcAge(2000), 25);
});

// Creates a box with an equal height and width
test('creates a box', async function(t) {
    await t.test('creates a small box', function() {
        assert.equal(createBox(10, 10), 100);
    });

    await t.test('creates a large box', function() {
        assert.equal(createBox(50, 50), 2500);
    });
});

// Checks to see whether or not the person has a full driving licence
test('checks license', function() {
    return assert.match(`${canDrive()}`, /Full Driving Licence/);
});

// Confirms that the person has a power level that is over 9000!
test('confirms power level', function() {
    return assert.ok(powerLevel());
});

// Checks to see if the employees have the same amount of shift work days in a week
test('employees have an equal number of work days', function() {
    const employeeOne = ['Monday', 'Tuesday', 'Wednesday,', 'Thursday'];
    const employeeTwo = ['Friday', 'Saturday', 'Sunday,', 'Monday'];
    return assert.equal(workSchedule(employeeOne.length, employeeTwo.length), 8);
});

// test non existence
test('confirms adder', function(a, b) {
    return assert.equal(adder(7, 17), 23);
});

Here comes the software containing the units, functions to be tested. Please notice that this program is oblivious to the existence of the test while the test was very much aware of the functions in this program.

Example 2. A Program to be Tested, logrocketjs/index.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const calcAge = function(dob) {
    let digits = {
        year: 'numeric',
    };

    let year = new Date().toLocaleDateString('en-US', digits);
    console.log(year);
    return year - dob;
};

const createBox = function(x, y) {
    return x * y;
};

const canDrive = function() {
    let age = 18;
    if (age >= 18) {
        return 'Full Driving Licence';
    } else {
        return 'Provisional License';
    }
};

const powerLevel = function() {
    let power = 9001;

    if (power > 9000) {
        return true;
    } else {
        return false;
    }
};

const workSchedule = function(employeeOne, employeeTwo) {
    return employeeOne + employeeTwo;
};

module.exports = {
    calcAge,
    createBox,
    canDrive,
    powerLevel,
    workSchedule,
};

The Test Runner is executed from the CLI by

1
2

node --test

The Test Runner will recursively search the current directory for any of:

Return to main lesson